• 15
• 15
• 11
• 9
• 10

# Cin Structure

This topic is 3813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
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
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
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
Thanks a lot for the help, the suggestions and the positive support [smile]

##### Share on other sites
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
Quote:
 if (x > 5);
Double-check the syntax of the above line...

##### Share on other sites
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;}