Cin Structure

Recommended Posts

GamerTom    122
Hey, as I posted in another post; I am a beginner programmer and just started learning C++. I was fooling around with it and im trying to write a simple program where I can use Cin to input numbers and add them with already set variables. What is the whole structure of such a program, What kind of header and main function....and so on. Just to make myself clearer im trying to do this: float num1; float num2; float total; total = num1 + num2 cout << "Enter number" cin >> num1 and so on...and im having a hard time, so whats the set up?

Share on other sites
Well, first you think what you do need?

Well, you obviously need iostream. Now what variables do you need? You described that up top. 3 variables. The first number, second number, and total of them added up.

Now, you need to process input. You process the first number. Then, the second number. The total is the first number they entered + the second number they entered. You would then want to output the total variable.

float num1;float num2;float total;cin>>num1;cin>>num2;total=num1+num2;cout<<total;

Just a little example to give you a head start.

Share on other sites
GamerTom    122
Thank you chad, well I wrote this and it worked. Can anyone tell me if they see any bad habits in the code?

#include <iostream>

int main()
{

float num1;
float num2;
float total;

std::cout <<"Enter first number ";
std::cin >> num1;
std::cout << "Enter second number ";
std::cin >> num2;

total = num1 + num2;

std::cout << "The answer is " << total << std::endl;

}

Share on other sites
Roboguy    794
Quote:
 Original post by GamerTomThank you chad, well I wrote this and it worked. Can anyone tell me if they see any bad habits in the code?#include int main(){ float num1; float num2; float total; std::cout <<"Enter first number "; std::cin >> num1; std::cout << "Enter second number "; std::cin >> num2; total = num1 + num2; std::cout << "The answer is " << total << std::endl;}

I'd say it looks pretty good, except the variable total is not needed. Also, I added [code] tags to the appropriate part of your post in the quote. You might want to learn about them.

Share on other sites
Crypter    748
I would recommend to initialize all variables as soon as they are declared. Also, as the above poster mentioned, you do not need the total variable.

Also, you specify that the routine returns an int, but do not return anything. While the complier silently returns 0, you should not rely on the complier, as it will not do that for all routines.

For example, here is a modified version of your code with all of the above:
#include <iostream>int main(){	float num1=0.0f;        // variables are initialized to 0	float num2=0.0f;	std::cout <<"Enter first number   ";	std::cin >> num1;	std::cout << "Enter second number   ";	std::cin >> num2;	std::cout << "The answer is   " << num1 + num2 << std::endl;        return 0; // return 0 to operating envirement}

Other then that, it looks good to me. Great job too![smile]

Share on other sites
GamerTom    122
Thanks a lot for the help, the suggestions and the positive support [smile]

Share on other sites
GamerTom    122
Another problem: Im trying to work with if and else statments and I am trying to write something so I can input a number and then compare that to x > 5 and then output the correct statment under if or else. Here is my source code
#include <iostream>int main(){		int x;		std::cout << "What number is great then 5  ";	std::cin >> x;			if (x > 5);	{		std::cout << "statment is true" << std::endl;	}	else	{		std::cout << "statement is false" << std::endl;	}	}

now the error message its giving me is: illegal else without matching if

Share on other sites
jyk    2094
Quote:
 if (x > 5);
Double-check the syntax of the above line...

Share on other sites
monp    100
Here you go.
Quote:
 #include int main(){ int x; std::cout << "What number is great then 5 "; std::cin >> x; if (x > 5) { std::cout << "statment is true" << std::endl; } else { std::cout << "statement is false" << std::endl; } return 0;}

Share on other sites
GamerTom    122
It works monp! Thank you....but I dont see what was wrong... except for the return0;.....was that the problem?

Share on other sites
Wolfdog    268
Quote:
Original post by jyk
Quote:
 if (x > 5);
Double-check the syntax of the above line...
Quote:
 Original post by GamerTomIt works monp! Thank you....but I dont see what was wrong... except for the return0;.....was that the problem?
Semicolon at the end of the if statement.

Share on other sites
Roboguy    794
Quote:
 Original post by GamerTomi did have a semi colon: if (x > 5); you mean like that? Cause that is in my source code.

That is the problem; you shouldn't have one there.

Share on other sites
Feralrath    163
semi colons go after declaring values, calling functions and at the end of classes. by putting the semi colon at the end of the line declaring the if statement you basically say; this is the end don't do anything with it.

Share on other sites
daviangel    604
Quote:
 Original post by Feralrathsemi colons go after declaring values, calling functions and at the end of classes. by putting the semi colon at the end of the line declaring the if statement you basically say; this is the end don't do anything with it.

Yup welcome to your first "logic" bug which the compiler won't catch since what you accidently did was write a null statement which is legal and useful as a placeholder;)

The Null Statement

The "null statement" is an expression statement with the expression missing. It is useful when the syntax of the language calls for a statement but no expression evaluation. It consists of a semicolon.

Null statements are commonly used as placeholders in iteration statements or as statements on which to place labels at the end of compound statements or functions.

Share on other sites
GamerTom    122
Is it possiable to set a variable to a word or letter? Because im trying to program kind of a calculator that adds and subtracts. So I wannt put someting like Type add for addition and so on for subtraction. Something kind of like this:

float num1 = 0.0; 	float num2 = 0.0;	float total;	float total2;	float add;	float sub;	float x;	std::cout << "Type add for addition, type s subtracts:  ";	std::cin >> x;	if (x == add)	{				std::cout << "Enter first number: ";		std::cin >> num1;		std::cout << "Enter second number: ";		std::cin >> num2;		total = num1 + num2;		std::cout << num1 << " + " << num2 << " = " << total << std::endl;}

Share on other sites
Dom_152    476
Quote:
 Original post by GamerTomIs it possiable to set a variable to a word or letter? Because im trying to program kind of a calculator that adds and subtracts. So I wannt put someting like Type add for addition and so on for subtraction. Something kind of like this: *** Source Snippet Removed ***

It's certainly possible. To do so you'll need to use character strings. A float can't hold characters only floating point numbers (As the name suggests) so your variable x would either have to be char * or a std::string (Which can be used by including the string header in your program). std::string is generally preferred by C++ programmers as there is no messing about with pointers and just a lower chance of anything going horribly wrong. If you want to compare your string variable to a literal value you must have the value contained in double quotes:

if (x == "add")

Hope this helps.

EDIT: As you're using strings now your two variables named add and sub are redundant so you can get rid of those. As people have mentioned above your total variable is also not needed. Neither is total2 (Why is it there anyway?). You can do the same thing like this:

std::cout << num1 << " + " << num2 << " = " << num1 + num2 << std::endl;

Share on other sites
Zahlman    1682
Variables in C++ have a type. This means the same thing that it does in ordinary English. When you write 'float thing;', you are saying "'thing' is a variable, and it is a 'float' (i.e. its *type* is 'float')".

To represent text, use the standard library type std::string. It comes from the <string> library header.

Reading into a string with the >> technique will only read a word at a time. If you want the user's entire line of input, use the free function std::getline:

using namespace std;string s;getline(cin, s);

Share on other sites
GamerTom    122
Thanks a lot but can you show me an example so i can see it clearly, its giving me an error message that says add is being used without being defined, this is how i have it

         char add;	char sub;	char x;	std::cout << "Type add for addition, type s subtracts:  ";	std::cin >> x;	if (x == add)	{			      std::cout << "Enter first number: ";	      std::cin >> num1;	      std::cout << "Enter second number: ";	      std::cin >> num2;	      total = num1 + num2;	      std::cout << num1 << " + " << num2 << " = " << total << std::endl;	      std::cout << "-------------------------- " << std::endl;	}

Share on other sites
Crypter    748
*edit:

I apologize, I misread your original question. This is what he was referring to:
#include <iostream>#include <string>   // Include <string>int main () {	char sub=' ';  // always initialize	std::string x (" ");  // here is your string type (Initialized to a space)// Notice this is of type "string"	std::cout << "Type add for addition, type s subtracts:  ";// Here we get a line from the console buffer (stdin). Whatever you type will be// stored in your string variable, x.	getline (std::cin, x);  // get line from stdin// Now, we compare the string variable x, with the string constant "add" 	if (x == "add")	{		              float	num1=0.0f;              float	num2=0.0f;              float	total=0.0f;	      std::cout << "Enter first number: ";	      std::cin >> num1;	      std::cout << "Enter second number: ";	      std::cin >> num2;	      total = num1 + num2;	      std::cout << num1 << " + " << num2 << " = " << total << std::endl;	      std::cout << "-------------------------- " << std::endl;	}	return 0;}

Share on other sites
GamerTom    122
Thanks a lot crypter, am i suppose to add more header files to your library? Cause I tried to compile source code for a guess the number code I found online and the header file was <iostream.h> and an error came up saying that iostream.h was not in the library or something like that. If im suppose to add the header files, how do I get them?

Share on other sites
Crypter    748
Quote:
 Original post by GamerTomThanks a lot crypter, am i suppose to add more header files to your library? Cause I tried to compile source code for a guess the number code I found online and the header file was and an error came up saying that iostream.h was not in the library or something like that. If im suppose to add the header files, how do I get them?

All of the headers mentioned in this post is from the Standard C++ Library and the C++ Runtime (CRT) DLL's. Your complier should already have them.

iostream.h is a old C++ header that is only there for backward compatibility with older compliers, don't use it. <iostream> is the correct header.

Share on other sites
GamerTom    122
if there away to connect 2 ifs to one else?

Share on other sites
Crypter    748
Quote:
 Original post by GamerTomif there away to connect 2 ifs to one else?

Of course:
if (condition) {}else if (condition) {}else {}

Share on other sites
Zahlman    1682
Quote:
 Original post by GamerTomThanks a lot but can you show me an example so i can see it clearly,

Quote:
 Original post by Dom_152If you want to compare your string variable to a literal value you must have the value contained in double quotes:if (x == "add")

Quote:
 Original post by ZahlmanTo represent text, use the standard library type std::string. It comes from the library header.

Quote:
 Original post by GamerTomits giving me an error message that says add is being used without being defined, this is how i have it char x; if (x == add)

I don't understand. Are you in fact reading what we're telling you here? Where did you get 'char' from?

Share on other sites
sheep19    494
Quote:
 Original post by GamerTomThanks a lot but can you show me an example so i can see it clearly, its giving me an error message that says add is being used without being defined, this is how i have it*** Source Snippet Removed ***

So you have:

char add;char x;std::cin >> x;if (x == add){//blah blah blah}

The variable that it's not defined is add;

try:
char add = 'y';char x;std::cin >> x;if (x == add){//blah blah blah}

it should be fine now.