Help With Cin Statement
4 posters
:: C Languages :: C++ :: Help!
Page 1 of 2
Page 1 of 2 • 1, 2
Help With Cin Statement
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
Re: Help With Cin Statement
- 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
Re: Help With Cin Statement
Click on the button, and when the the BBCode tags that say, "CODE" appear, paste your code inside those tags.Belso wrote:it wont put my whole code.. help
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.
Re: Help With Cin Statement
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:
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
- 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- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
it sorta helps me
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
Re: Help With Cin Statement
Strange...it looks fine to me. I will look at it later.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;
}
Re: Help With Cin Statement
missing some code. I'll add some comments to clarify what I edited
looks ok... yeah right DB. Anyways hope this helps these where simple C++ errors you should have fixed this via error msgs.
- 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- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: Help With Cin Statement
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.Nighthawk0973 wrote:looks ok... yeah right DB.
?
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
Re: Help With Cin Statement
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:
Anyways that's the code debugged, just to prove to you it's debugged properly I'll run it in codeblocks.
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- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Thank you
- 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;
}
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
Re: Help With Cin Statement
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.
Re: Help With Cin Statement
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.
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- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: Help With Cin Statement
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.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.
Re: Help With Cin Statement
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
Yeah they used Dev-C++ in that version, 7 books in one however, never got around to finishing the first 'book'
Yeah they used Dev-C++ in that version, 7 books in one however, never got around to finishing the first 'book'
Nighthawk0973- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: Help With Cin Statement
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 . 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.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
Yeah they used Dev-C++ in that version, 7 books in one however, never got around to finishing the first 'book'
Re: Help With Cin Statement
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
Re: Help With Cin Statement
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.
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- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: Help With Cin Statement
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
Re: Help With Cin Statement
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.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: Help With Cin Statement
It probably was a 404 error. That happened to me once. Did you get a 404 when you hit submit?legolizard wrote:Also David B. I posted something earlier on this thread but I don't see it.
Page 1 of 2 • 1, 2
:: C Languages :: C++ :: Help!
Page 1 of 2
Permissions in this forum:
You cannot reply to topics in this forum
|
|