Would you like to react to this message? Create an account in a few clicks or log in to continue.

Help With Cin Statement

4 posters

 :: C Languages :: C++ :: Help!

Page 1 of 2 1, 2  Next

Go down

Help With Cin Statement Empty Help With Cin Statement

Post  Belso Mon Aug 22, 2011 1:26 am

Hello i made a program and ive gotten the beginning dialogue to work and now i just need help with the cin part. Whats wrong with this program?

Code:
#include

using namespace std;

int main()
{
int userinp;

cout


Last edited by David B on Mon Aug 22, 2011 9:41 am; edited 4 times in total (Reason for editing : Programming Code Placed Inside BBCode "Code" Tags)

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Belso Mon Aug 22, 2011 1:28 am

Code:
#include

using namespace std;

int main()
{
int userinp;

cout


Last edited by David B on Mon Aug 22, 2011 9:33 am; edited 1 time in total (Reason for editing : Programming Code Placed Inside BBCode "Code" Tags)

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Belso Mon Aug 22, 2011 1:28 am

it wont put my whole code.. help

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Mon Aug 22, 2011 9:31 am

Belso wrote:it wont put my whole code.. help
Click on the Help With Cin Statement Quote10 button, and when the the BBCode tags that say, "CODE" appear, paste your code inside those tags.



As for the rest of your problem, I am not sure what exactly you want help with. You have two pieces of code, and I am not sure which one to look at. Could you elaborate?



Also, I am splitting this topic off into it's own topic. This is an issue that deserves a seperate thread.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty ok thanks

Post  Belso Mon Aug 22, 2011 2:00 pm

All right so where should i post my code?

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Mon Aug 22, 2011 2:16 pm

Belso wrote:All right so where should i post my code?
What do you mean?
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Nighthawk0973 Mon Aug 22, 2011 3:25 pm

Alright so your obviously using the cin to get data from the user to input the value of an interger variable. Here's how you do it:

Code:

#include <iostream>

using namespace std;

int main(){

int userInt;
cout << "Please enter your age: " << endl; //This simple cout statement creates a prompt for the user.
cin >> userInt; //As you can see, the cin statement is similar to the cout statement, however instead of
//having two '<' symbols you have 2 '>' symbols. Instead of entering the variable to print you enter the
//variable to enter the value of. In this case, 'userInt'. Now let's just finish this program by printing userInt
cout << userInt << endl; //the 'endl' is for End Line and is used for good looks when reading your program
}

The '//' is a comment, read the text after the comments because any text AFTER a comment is ingnored by the compiler, I use this to explain things to you. I hope you found this helpful!

Anyways, the code tags work like this, click the 'Code' tag button, type your code, and than click it again and it will make a /code. Everything in between the code and /code is a code tag. I couldn't use the square brackets because it would mistaked those as real code tags.

Again, hope this helps, please stick around this forums if you need help, it's a really great place to learn C++!
-Nighthawk0973
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Help With Cin Statement Empty it sorta helps me

Post  Belso Mon Aug 22, 2011 6:03 pm

I read everything you said and it is useful information but it does not help me in what i want to accomplish. What i wish to do is ask them for a value for userinp like a or something and then i want it to say something depending on what is said like this
Code:
#include

using namespace std;

int main()
{
int userinp;
cout<< "how old are you?" endl
cin >> userinp
if userinp = 1
cout << 1 is really young

return 0;
}


Last edited by David B on Mon Aug 22, 2011 6:38 pm; edited 1 time in total (Reason for editing : BBCode Formatting Messed Up)

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Mon Aug 22, 2011 6:40 pm

Belso wrote:I read everything you said and it is useful information but it does not help me in what i want to accomplish. What i wish to do is ask them for a value for userinp like a or something and then i want it to say something depending on what is said like this
Code:
#include

using namespace std;

int main()
{
int userinp;
cout<< "how old are you?" endl
cin >> userinp
if userinp = 1
cout << 1 is really young

return 0;
}
Strange...it looks fine to me. I will look at it later.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Nighthawk0973 Tue Aug 23, 2011 12:34 pm

missing some code. I'll add some comments to clarify what I edited

Code:

#include <iostream> //include what? Include iostream

using namespace std;

int main(){ //rearanged the curly brace to a way I like it to be. You don't have to do this.
int userinp;
cout<< "how old are you?" << endl; //added a '<<' before the endl and a semicolon.
cin >> userinp; //added semicolon
if (userinp == 1){    //I added parenthasis, and replaced the '=' with '==' cause that's what C++ uses
//I also added a curly brace on the above line so we could start the if code.
cout << "1 is really young" << endl; //I added quotation marks and an endl statment on this line.

}
return 0; //This shouldn't go inside of the if statement.

looks ok... yeah right DB. Anyways hope this helps these where simple C++ errors you should have fixed this via error msgs.
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Tue Aug 23, 2011 1:22 pm

Nighthawk0973 wrote:looks ok... yeah right DB.
I think I was half asleep when I posted that because even before reading this, I re-read the code, and saw that it was NOT okay. drunken
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty ?

Post  Belso Tue Aug 23, 2011 5:32 pm

i tried everything you said and it was amazing information but its still not working
Code:
#include <iostream>

using namespace std;

int main{)
{
int userinp;
cout<< "This is a c++ tutorial that teaches you some commands" << endl;
cout<< "commonly used in c++." << endl;
cout<< "This information is from Nighthawk and Legolizard." << endl;
cout<< " To use this program type the letter in front of the command << endl;
cout<< "wish to learn."
cout<< "cout(A)"
cin >> userinp;
if (userinp == a) {
cout<< "Cout stands for Console output and outputs what you code it to."<< endl;
cout<<" To use cout you would do this."<<endl;
cout<<"cout<< (quotation marks)Hello this is how you do a cout statement" <<endl;
cout<<"to start the next line of output do << endl; that is a L not a 1" <<endl;
}
return 0;

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Nighthawk0973 Tue Aug 23, 2011 9:08 pm

dude, even that code doesn't work. It's little things like forgetting to add a quotation mark that error out on the compiler for C++. With Alice you can't make errors, the errors are all stamped out before you can even finish making them. You could place an object with some errors like a missing texture, because alice will ask you if you want to create the model, but remove the texture since the texture cannot be found. You can't choose to create the model and still have the texture there.

Revised version of this code:
Code:

#include <iostream> //good

using namespace std; //good

int main(){ // bad. Original code: 'int main({'
//extra curly brace was here, remove this.
char userinp; //good, but bad at the same time
//you made this variable an int, but in the if
//statement for if to be 'A' but an int is an interger
//not a character or 'char'
cout<< "This is a c++ tutorial that teaches you some commands" << endl; //good
cout<< "commonly used in c++." << endl; //good
cout<< "This information is from Nighthawk and Legolizard." << endl; //good
cout<< " To use this program type the letter in front of the command" << endl; //Added second quote
cout<< "wish to learn."; //forgot semicolon
cout<< "cout(A)"; //again, no semicolon
cin >> userinp; //good
if (userinp == 'a') { //good, but when defining char values you use 2 ' symbols
cout<< "Cout stands for Console output and outputs what you code it to."<< endl; //good
cout<<" To use cout you would do this."<<endl; //good
cout<<"cout<< (quotation marks)Hello this is how you do a cout statement" <<endl; //good
cout<<"to start the next line of output do << endl; that is a L not a 1" <<endl; //good
} //good, you ended the if statment
return 0; //good you told the program to return 0
} //bad. You forgot the end the int main function, with this curly brace. XD

Anyways that's the code debugged, just to prove to you it's debugged properly I'll run it in codeblocks.
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Help With Cin Statement Empty Thank you

Post  Belso Tue Aug 23, 2011 10:07 pm

Code:
#include <iostream>

using namespace std;

int main(){
char userinp;
cout<< "This is a c++ tutorial that teaches you some commands" << endl;
cout<< "commonly used in c++." << endl;
cout<< "This information is from Nighthawk and Legolizard." << endl;
cout<< " To use this program type the letter in front of the command" << endl;
cout<< "wish to learn.";
cout<< "cout(A)";
cin >> userinp;
if (userinp == 'a') {
cout<< "Cout stands for Console output and outputs what you code it to."<< endl;
cout<<" To use cout you would do this."<<endl;
cout<<"cout<< (quotation marks)Hello this is how you do a cout statement" <<endl;
cout<<"to start the next line of output do << endl; that is a L not a 1" <<endl;
system("PAUSE");
}
return 0;
}
Alright it works now and i added a system("pause"); to make it readable and i would like to know if there was a way to clean it up so like when enter is hit it clears the original paragraph and puts the new paragraph in its place? is that possible?


Last edited by Belso on Tue Aug 23, 2011 10:09 pm; edited 4 times in total (Reason for editing : to add code)

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Wed Aug 24, 2011 2:06 pm

I think you can, but it involves character encoding. I read about it in C++ for dummies, but I don't remember how it's done.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Nighthawk0973 Wed Aug 24, 2011 4:45 pm

That's basically keyinput. Which is a bit advanced. You could make something like that if you knew how to clear the screen but I'm still learning console applications and I don't even know how to do that.

Anyways glad it worked out for ya. Are you using dev C++ or are you using Code::Blocks (asking both of you) because Dev-C++ is what the dummies book uses and if you switch the Code::Blocks you'll be able to code in C++ easier. Code::Blocks uses a wide variety of compilers, I wish I could use MSVC++, but not use Microsofts compiler, use the Compiler I use with Code::Blocks, that way there'll be code that makes mroe sense to me, but I'll have the power of Microsofts GUI creation tool.
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Wed Aug 24, 2011 4:48 pm

Nighthawk0973 wrote:That's basically keyinput. Which is a bit advanced. You could make something like that if you knew how to clear the screen but I'm still learning console applications and I don't even know how to do that.

Anyways glad it worked out for ya. Are you using dev C++ or are you using Code::Blocks (asking both of you) because Dev-C++ is what the dummies book uses and if you switch the Code::Blocks you'll be able to code in C++ easier. Code::Blocks uses a wide variety of compilers, I wish I could use MSVC++, but not use Microsofts compiler, use the Compiler I use with Code::Blocks, that way there'll be code that makes mroe sense to me, but I'll have the power of Microsofts GUI creation tool.
I use Code::Blocks. You must have an older edition of the C++ For Dummies book because I have the newest one, and the newest one uses Code::Blocks.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Nighthawk0973 Wed Aug 24, 2011 4:55 pm

Really? I actually don't own it, got it from the library lol. I never buy books about programming when I can sit around in the area where people never seem to go, the programming section Very Happy

Yeah they used Dev-C++ in that version, 7 books in one however, never got around to finishing the first 'book' Wink
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Wed Aug 24, 2011 4:59 pm

Nighthawk0973 wrote:Really? I actually don't own it, got it from the library lol. I never buy books about programming when I can sit around in the area where people never seem to go, the programming section Very Happy

Yeah they used Dev-C++ in that version, 7 books in one however, never got around to finishing the first 'book' Wink
I know. When I go to the mall with my parents, and they have a Borders or Barnes & Noble in the mall (Borders is going out of business, so I won't get to do this anymore frown. This is because none of our malls have Barnes & Noble.), I just ask them if they can let me hang out in "nerd-land", and they usually say yes.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Belso Wed Aug 24, 2011 5:27 pm

I have to use dev because code blocks does not work on my computer for some reason but i would like you to check out my tutorial program when i get it posted

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  legolizard Wed Aug 24, 2011 5:56 pm

Yes you can :


system("CLS");



However I don't reccomend EVER using the system() command mainly because my computer has feelings and I don't want to make it do anything it doesn't have to.



Also David B. I posted something earlier on this thread but I don't see it.
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Belso Wed Aug 24, 2011 6:29 pm

So is that the only way because you said you recommend never using it so is there a better way

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  legolizard Wed Aug 24, 2011 7:17 pm

Yes there are a few other methods that you can do the "clear the screen." However all or most of them are difficult. So this method is the easiest but it is not the best but for now it should suffiice. Just don't get into the habit of using the system() command it is very resaource heavy (My computer pouts evertime I use it ) but for now you shouldn't need to worry TOO much about that. Laughing
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Belso Wed Aug 24, 2011 8:17 pm

alright cool

Belso

Posts : 18
Join date : 2011-08-22

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  David B Wed Aug 24, 2011 11:19 pm

legolizard wrote:Also David B. I posted something earlier on this thread but I don't see it.
It probably was a 404 error. That happened to me once. Did you get a 404 when you hit submit?
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Help With Cin Statement Empty Re: Help With Cin Statement

Post  Sponsored content


Sponsored content


Back to top Go down

Page 1 of 2 1, 2  Next

Back to top


 :: C Languages :: C++ :: Help!

 
Permissions in this forum:
You cannot reply to topics in this forum