-
November 20th, 2011, 06:00 PM
#1
Help a newbi
Ok so i'm trying to write something very simple, I'm a complete newbi.
#include <iostream>
using namespace std;
int age;
char answer;
int main()
{
cout << "Hello, And welcome to my Quiz, This is just a basic Program\n\n";
system("PAUSE");
system("CLS");
cout << "Please enter your Age\n";
cin >> age;
cout << "So you are " << age << " Years old?, Y/N \n";
cin >> answer;
if(answer == "Y" || answer == "y")
{
cout << "Ok ";
}
system("PAUSE");
}
If you press N i wan't it to go Back to where you enter your age, How do i send it back to that line?, Or do i just ask the question again?
Last edited by UpcomingChris; November 20th, 2011 at 06:09 PM.
-
November 20th, 2011, 06:15 PM
#2
Re: Help a newbi
You can use for instance a do-while loop.
An observation: answer == "Y" compares if answer is the same as the address of the literal "Y". You should use 'Y' to compare with a character.
Have you tried to compile this yet? I bet the compiler has some things to tell you...
Edit: If you wrap your code inside code tags the indentation is preserved. [code]Your code here[/code]
Last edited by S_M_A; November 20th, 2011 at 06:17 PM.
-
November 20th, 2011, 06:16 PM
#3
Re: Help a newbi
Originally Posted by S_M_A
You can use for instance a do-while loop.
An observation, answer == "Y" compares if answer is the same as the address of the literal "Y". You should use 'Y' to compare with a character.
Have you tried to compile this yet? I bet the compiler has some things to tell you...
indeed it did, i edited it a little bit and it's all working, just don't know how to do the loop, Can you explain it to me?
#include <iostream>
#include <string>
using namespace std;
int age;
string answer;
int main()
{
cout << "Hello, And welcome to my Quiz, This is just a basic Program\n\n";
system("PAUSE");
system("CLS");
cout << "Please enter your Age\n";
cin >> age;
cout << "So you are " << age << " Years old?, Y/N \n";
cin >> answer;
if(answer == "Y" || answer == "y")
{
cout << "Ok ";
}
else
{
cout << "Ok, Please enter your correct age.\n";
}
system("PAUSE");
}
-
November 20th, 2011, 06:20 PM
#4
Re: Help a newbi
Your first concern rather should be whether what you currently have actually compiles (and works), and this certainly does not:
Originally Posted by UpcomingChris
Code:
if(answer == "Y" || answer == "y")
You can't extract data from cin into string literals, the first and probably most obvious reason for this being that these literals are read-only. Instead you'd need to read the data from cin into some variable and then compare that to your literals.
I do get what you want to express with this line, but the compiler is comparatively dumb and stubborn and insists on you using proper C++ syntax and semantics. The compiler entirely lacks any sort of fantasy or imagination.
If you press N i wan't it to go Back to where you enter your age, How do i send it back to that line?, [...]
Obviously, you need a loop for that.
[...] Or do i just ask the question again?
I wouldn't recommend that approach. Imagine you need to accomodate to a stubborn user who enters the wrong age, say, 20 or so times (and each time admits the mistake). That would obviously be pretty tideous for you as the programmer. And then, when you're done with the program, comes an even denser user who does that 21 times...
And please use code tags when posting code.
EDIT: Note: This post is based on the version of the OP prior to the edit from 12:09 AM CET.
Last edited by Eri523; November 20th, 2011 at 06:23 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 20th, 2011, 06:26 PM
#5
Re: Help a newbi
Originally Posted by UpcomingChris
[...] just don't know how to do the loop, Can you explain it to me?
http://www.cplusplus.com/doc/tutorial/control/
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 20th, 2011, 06:26 PM
#6
Re: Help a newbi
I'm not extracting a cin straight into literals, I'm extracting the cin into a String, and then extracting the String into 'literals' aren't i?, or am i looking at it wrong?
-
November 20th, 2011, 06:27 PM
#7
Re: Help a newbi
Thanks for the link, now to get it to work with my code!, Going to get the hang of this sometime soon...
-
November 20th, 2011, 06:29 PM
#8
Re: Help a newbi
Please don't edit your posts after you get an answer (at least not vital parts).
Eri523 gave you a good link regarding the do-while
-
November 20th, 2011, 06:31 PM
#9
Re: Help a newbi
Originally Posted by UpcomingChris
I'm not extracting a cin straight into literals, [...] or am i looking at it wrong?
It's relatively late at night right now here, but I'm pretty sure you did prior to editing your post. However, I just noticed that even my quote from your post in post #4 already is the fixed version. So I don't have any proof...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 20th, 2011, 06:34 PM
#10
Re: Help a newbi
Originally Posted by S_M_A
Please don't edit your posts after you get an answer (at least not vital parts).
Eri523 gave you a good link regarding the do-while
i didn't, I edited my post before any comment, to my knowledge, was left. And i thank'd him for the link. And i have a new problem!.
Code:
#include <iostream>
#include <string>
using namespace std;
int age;
string answer;
int main()
{
cout << "Hello, And welcome to my Quiz, This is just a basic Program\n\n";
system("PAUSE");
system("CLS");
cout << "Please enter your Age\n";
cin >> age;
cout << "So you are " << age << " Years old?, Y/N \n";
cin >> answer;
while(answer == "N" || answer == "n")
{
cout << "Please enter your correct age ";
}
cout << "Ok\n";
system("PAUSE");
}
It just spams "Please enter your correct age", I guess it's becasue once you assign n to 'answer' it won't change unless you change it again, which i don't know how to do yet......?
-
November 20th, 2011, 06:36 PM
#11
Re: Help a newbi
This is the visual studio forum, have you tried running this in the debugger? I'm not sure what I respond to anymore... you know you can preview your post i.e. completing it before actually posting it?
Yes, you have to re-assign answer to break the loop. I would have done it something like this (the break variant just because you had two different outputs before reading the age)
Code:
cout << "Please enter your Age\n";
while(1)
{
cin >> age;
cout << "So you are " << age << " Years old?, Y/N \n";
cin >> answer;
if( answer == "Y" || answer == "y" ) break;
cout << "Please enter your correct age ";
}
Last edited by S_M_A; November 20th, 2011 at 06:48 PM.
-
November 20th, 2011, 06:40 PM
#12
Re: Help a newbi
Apologies, i seem to be in the wrong part, i'm using DevC++, But can you suply the solution to leave the loop?
-
November 20th, 2011, 06:56 PM
#13
Re: Help a newbi
If remember correctly DevC++ is not maintained anymore?
Looking at their homepage they say it's using gcc 3.4.2 and that was released September 6, 2004! Get yourself a more modern compiler like MS Visual Studio Express for instance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|