Sign in to follow this  

Cin Structure

This topic is 3715 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
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
Quote:
Original post by jyk
Quote:
if (x > 5);
Double-check the syntax of the above line...
Quote:
Original post by GamerTom
It 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 this post


Link to post
Share on other sites
Quote:
Original post by GamerTom
i 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 this post


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


Link to post
Share on other sites
Quote:
Original post by Feralrath
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.

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


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


Link to post
Share on other sites
Quote:
Original post by GamerTom
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:

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


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


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


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


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


Link to post
Share on other sites
Quote:
Original post by GamerTom
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?


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


Link to post
Share on other sites
Quote:
Original post by GamerTom
Thanks a lot but can you show me an example so i can see it clearly,


Quote:
Original post by Dom_152
If 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 Zahlman
To represent text, use the standard library type std::string. It comes from the <string> library header.


Quote:
Original post by GamerTom
its 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 this post


Link to post
Share on other sites
Quote:
Original post by GamerTom
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

*** 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.

Share this post


Link to post
Share on other sites

This topic is 3715 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this