Jump to content
  • Advertisement
Sign in to follow this  
GamerTom

Cin Structure

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!