C++ :: What Data Type To Treat Input As
Sep 3, 2013
I know that an int is usually 4 bytes, ranging from -2^31 to 2^31-1 for a signed int and 0 to 2^32-1 for an unsigned int. My question is simply, bit-wise (I know they are labelled in the code), how does it determine whether to show -2^31 or 2^32-1 if it was 11111111 11111111 11111111 11111111 in bits? Is there a 5th byte to tell the compiler what data type to treat the input as?
View 3 Replies
ADVERTISEMENT
Oct 15, 2013
I'm currently trying to solve a programming assignment and i got the logic of it, however i find it hard to implement.
What i need to do basically is fill an array with objects. Each object is a class that contains only one type of data. This means i can place int, double and string for example in one simple array.
However i can't figure out how to read data and then decide what it is. Even if i use templates once i call the function i have to give it a type, so getType<int> for example will not work with double or string.
I know about typeID and how to use it, i just can't figure out where to use it.
View 2 Replies
View Related
Apr 11, 2013
How would I check for proper data type when someone is to input a value into the program? Ex:
int i;
string a;
cout << "Enter a number: ";
cin >> i;
cout >> "Enter a string: ";
cin >> a;
How would you check to make sure that int i would be an actual number and not a letter like "a"?
View 4 Replies
View Related
Apr 17, 2015
Here's my code so far:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int dayNumber = 1;
[Code] ....
The code, when run, prompts the user to input an integer like this:
Code:
Day 1
Andy :>12
Bill :>7
Charlie :>15
Day 2
Andy :>5
Bill :>25
Charlie :>14
.
.
.
etc.
Ok, so the code prompts the user to enter an integer for each of the 3 persons and then increments the "Day" and so on... When the user enters a negative value (-1, -2, etc.), the loop is broken and the program ends. Alright, everything is hunky-dory so far. 2 things which I can't figure out.
1. when the user doesn't enter anything and just hits return, the cursor drops a line and is still awaiting an input but doesn't show the prompt "Andy :>", for example. How can I get it to repeat the last prompt when the user doesn't enter anything? Like:
Code:
Day 1
Andy :>
Andy :>
Andy :>12
Bill :>25
Charlie :>15
.
.
etc.
2. When the user enters a letter or a special character, the program blows up. How can I correct this?
I've tried changing the data type for the variable used for the input, tried using getline, get, etc. With my current beginner knowledge, I'm missing something. So how can I get this to work?
The program should allow only integers to be entered, while allowing a negative number to trigger the loop to break or the program to end, and while re-prompting the last person if the user entered an invalid input.
View 13 Replies
View Related
Mar 2, 2014
// this program gives random number output
#include <iostream>
#include <cstdlib>// contains function protype for rand
#include <iomanip>// for setw
using namespace std;
[code]....
what is the effect on output of program of different numbers input to the int data type named seed*/
View 1 Replies
View Related
Jun 17, 2014
deal with a failed input stream and type checking input?
I use:
cout << "
Enter a menu option: ";
while(!(cin >> menuGameMode))
{
cin.clear();
while (cin.get() != '
')
continue;
cout << "Enter a menu option: ";
}
Is this the "Best" way to do it?
EDIT: We're assuming input is expecting an int.
View 9 Replies
View Related
Feb 1, 2015
I'm trying to create a new data type Data declared as int. What comes up to my mind is to create a structure like
Code:
typedef struct Data {
int number;
} Data;
but when I need to use the data I need to go through the structure. Is this a good way to declare a new data type?
View 2 Replies
View Related
Apr 19, 2013
I want to input data into text file while not deleting the original data in the file and I use something as
ofstream writefile;
writefile.open("example1.txt");
if (writefile.is_open()) {
for(j=0; j<N; j++) {
[Code] ....
But this will delete the original data.
View 3 Replies
View Related
Sep 19, 2014
Originally I had to create a simple integer palindrome program that looped while the user entered 5 digit inputs (entering -1 stopped the loop). I did this using a conversion to string, reading the length to determine if the length was valid, and then reading the string forward and backwards inside of a while loop. (snippet below)
while( digitsEntered != -1)//Allow user to quit by entering -1 to end the loop
{
ostringstream convert;//conversion stream
convert << digitsEntered;//converted text from number goes in the stream
convertedString = convert.str();//store the resulting conversion to convertedString
[Code] ....
The next stage of this program was to do the same thing with strings instead of integers. However, the option to end the loop by entering -1 is still a requirement.
I think the way to do this is to first determining whether the input is a string or an integer, and if it is a string then read it and if it's an integer determine if it's -1. However, whenever I write code to do this, it converts strings to 0 so the string is not stored and cannot be read to determine if it is a palindrome. Is there a way to determine the type of input without converting it into a different type i.e. read string and then keep string or read number and keep number?
View 3 Replies
View Related
Jan 21, 2015
I want to create a new data type called an inf_t. It's basically infinity (which for C++ is 1.7e+308). The only reason I want this is because I want to overload the cout << operation to print out INF/inf. Should I do this in a struct?
Code: struct inf_t {
private:
double inf = 1.7e+308;
};
std::ostream& operator << (std::ostream &stream, inf_t inf) {
stream << "INF";
return stream;
}
View 4 Replies
View Related
Mar 4, 2014
I want to find the size of the data type with out using sizeof() operator.
View 9 Replies
View Related
May 18, 2014
If I am asked to declare a data type for Date which should be in the format DD/MM/YY, which data type should i use for it? Is there any data type known as Date in C?
View 2 Replies
View Related
Apr 9, 2015
Is there a way to define a 3 bit or 12 bit data type in C/C++. I need these for defining my own packet having different bit length fields.
View 1 Replies
View Related
Jun 12, 2013
I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring.
View 15 Replies
View Related
Jan 4, 2013
Working on a Project Euler problem and the question asks for the largest prime number that is a factor of 600851475143. As you can see, this is significantly larger than the maximum of a long data type, which maxes out at 2147483647.
I'm running on Windows 32, so int64 is not a valid option for me. It seems like I'll likely have to use a different language to solve this problem.
View 4 Replies
View Related
Feb 15, 2014
I know how to store numeric data using keywords int, long, float, and so on. I'm making my own program called "Who is your soul-mate".The only question I want to ask is what's the keyword for storing alphabet data? As you can see below on my source file. I want to replace "int" keyword with another keyword that can store alphabet data. It's all in standard C.
#include <stdio.h>
int soulm01, soulm02, soulm03;
int year_of_birth;
int main(void)
[Code].....
View 5 Replies
View Related
Apr 7, 2014
In my main I have a do/while loop, which is to iterate around the method calculateImportance while i is not equal to the numNodes.
do
{
int i;
int numNodes = Test.count();
for(i=0; i<=numNodes; i++)
[Code]....
I have been messing around with the data type node, hence why it is there are present, but, that doesn't seem to the answer
This is the function declaration in the header file.
node CalculateImportance();
View 1 Replies
View Related
Jun 24, 2014
C# Code
[DllImport("dllTestForm.dll", EntryPoint = "showFormC")]
static extern void testShowFormC(byte[] photo,int len);
private void button6_Click(object sender, EventArgs e) {
Bitmap bmp = (Bitmap)Image.FromFile(@"d:11.jpg");
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
[Code] ....
The program has no tip errors ,But there is no any picture display in c++ form,Tested the simple data types, such as int, can be normal .
View 1 Replies
View Related
Jan 22, 2014
Ok so I have this simple program that gets input from a user. I just want to put in a line of code to make sure that hte user can't type in something like "pizza" , I want to make it say that if the user puts in something that is NOT a number they will get a error back saying "Wrong! try again!" Here is my code :
#include <iostream>
using namespace std;
//Summation Program
//Function Prototypes
int get_num();
void compute_sum(int num, int &sum);
[Code] ....
View 6 Replies
View Related
Sep 4, 2014
Assume the user has already put in the number of students (hence my variables numStuds, which will most likely be irrelevant to my problem).
So suppose I have this:
void inputStudentInfo(string *names, int *movies, const int numStuds) {
for(int i =0; i < numStuds; i++) {
cout << "Enter student name: "; getline(cin, names[i]); read_string(names[i]);
[Code] ....
Then I have my data type checking function:
//Data-Type Checking for strings
string read_string(string Sname) {
while(!cin.good())
[Code] ....
I am getting errors. I know the problem I think is that I am trying to data type check for a string made up of pointers* with just a string but I don't know how I am supposed to check this?
View 2 Replies
View Related
May 26, 2013
I want my method of the class to take as an input an array of data type derived from class. How can I do this?
Code:
class Planet {
public:
// typedef class Planet PlanetType;
void GetForce(int, PlanetType []);
};
Or when should I define my new data type PlanetType?
View 14 Replies
View Related
Mar 21, 2013
I program unix sokcet programming , and part of my code is ti open file from server and open it , but i surprised with this wierd error i dont have any reason for it ?
Code:
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
[Code] ....
File name is : myHeader.h
myHeader.h:24:1: error: unknown type name "File"???
View 1 Replies
View Related
Jul 8, 2013
I've seen some threads that include the information about what data types one has to use usually to put a number of 12digit. But honestly speaking none of them works for me & it takes me a lot suffering about the data type. Even I've googled but I didn't get any specific result.
So, which data type I'd have to use to enter 12digit number.
I'm writing a code to find the largest prime factor of the number 600851475143
View 4 Replies
View Related
Mar 9, 2014
Can you use data type double or float for an array? ie
double n[];
or
float a;
float m[a];
My code wont accept me changing the data type..will on accept int data type. I get the following error when I try to change the array to double or float..
3310E:C++vector.cpp[Error] invalid types 'double [1000][double]' for array subscript
View 4 Replies
View Related
Apr 26, 2014
So Im working on my semester prject for my programming class. It is to make a roulette game. And Im working on getting my table set up but the intellisensor in my Visual Studio seems to not be working and recognizing the string variable type. Its not changing to blue when I enter it.
This program is a roulette simulation, designed to be used for online gambling.
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <string>
#include <ctime>
using namespace std;
//Function Prototypes
void welcome();
double getBalance();
[Code] .....
When I run the program it gives me an error as well sometimes. This one: Unhandled exception at 0x0f681f68 (msvcp100d.dll) in Roulette Final Project.exe: 0xC0000005: Access violation reading location 0x8bb59d35.
Which opens another tab named "iosfwd" and points to this bit of code:
static int_type __CLRCALL_OR_CDECL to_int_type(const _Elem& _Ch)
{// convert character to metacharacter
return ((unsigned char)_Ch);
}
View 5 Replies
View Related
Aug 21, 2013
put enumerated data type in c++ for a poker program (specially for color and cardtype)?
Here are some codes:
//card.cpp
#include "cCard.h"
using namespace std;
cCard::cCard()
[Code]....
View 1 Replies
View Related