Cin Structure

Started by
24 comments, last by jpetrie 16 years, 6 months ago
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?
Advertisement
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.

Chad

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;

}
Quote:Original post by GamerTom
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;}


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.
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]
Thanks a lot for the help, the suggestions and the positive support [smile]
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
Quote:if (x > 5);
Double-check the syntax of the above line...
Here you go.
Quote:
#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;
}

return 0;
}
It works monp! Thank you....but I dont see what was wrong... except for the return0;.....was that the problem?

This topic is closed to new replies.

Advertisement