-
February 3rd, 2009, 09:45 PM
#1
Someone please help me out!!
Ok, I started learning the basics today, and I've run into a small problem. I spent hours looking for this on the internet, but I can't seem to find anything helpful.
The book I'm using is great as far as explaining the basic structure, and functions of programs, but It only uses numbers...
simply put:
I need to know how to create a variable that stores text that has been input by the user.
If someone could heplp me out on this one, I'm sure I can take it from there
Ty!
-
February 3rd, 2009, 10:02 PM
#2
Re: Someone please help me out!!
Wrong forum. This is a non-visual C++ issue, so you should have posted it here.
If your book doesn't show you how to get and store input as a string, it was probably never worth having in the first place... out of curiosity, what is the title of the book?
As for your question, start here.
[ NOTE - ovidiucucu ]
I just have merged the two threads.
Last edited by ovidiucucu; February 4th, 2009 at 03:40 AM.
-
February 3rd, 2009, 10:57 PM
#3
Please help! [Variables and text input]
I've searched the internet for tutorials on chars and strings that could help me do the following:
Prompt for input of a word, and, storing that word in a variable, use the variable to execute a certain command.
I need a very clear and thorough explanation! ( it might help if you try explaining in a way that you would to someone who can't even read ;D )
Any and all help will be greatly appreciated. Ty ;]
-
February 4th, 2009, 12:18 AM
#4
Re: Please help! [Variables and text input]
A basic example would be:
Code:
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string input;
do
{
cout << "Enter some text (\"exit\" to end):" << endl;
getline(cin, input);
cout << "You entered: " << input << endl;
} while (!cin.fail( ) && (input != "exit"));
return 0;
}
If you want something more than that, you're going to have to explain what you want in more detail.
-
February 4th, 2009, 12:23 AM
#5
Re: Please help! [Variables and text input]
Sorry about the bump, but I thought if I include what I've got so far it might help ppl to help me ;D
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int num1;
int num2;
int num3;
using namespace std;
#include <string>
using std::string;
int main() {
start:
cout << "Please type a number\n" << endl;
cin >> num1;
cout << "Please type another number\n" << endl;
cin >> num2;
num3 = num1 + num2;
cout << "The sum is " << num3 << endl;
back:
cout << "Would you like to continue?\n" << endl;
string answer; //Creates the string named "answer".
getline (cin, answer); //Adds whatever the user typed to the newly created string.
if (answer == "yes") { // Checks if what was typed was "yes".
string answer = ""; //Cleans the string, so that it won't cause an infinite loop.
goto start; //Returns to the beggining of the code.
else if (answer == "no") { //Checks if the user typed "no".
string answer = ""; //Cleans the string...
goto finish; //Goes to the end of the code.
else
cout << "Sorry, I didn't understand that.\n"
goto back; //Goes back to the prompt for continuance phase.
}
}
finish:
return 0;
}
Last edited by ovidiucucu; February 4th, 2009 at 03:39 AM.
Reason: addded [CODE] tags
-
February 4th, 2009, 01:57 AM
#6
Re: Please help! [Variables and text input]
Please post your well indented code in [code][/code] bbcode tags.
Now, the first thing that jumps out at me is the very unnecessary use of goto in your code. You should use a loop instead. Another bad habit that you may develop is the unnecessary use of global variables. It does not matter in this case since you only have the main function, but you should make them local variables anyway to develop a good habit.
Other potential bad habits include the using directive (using namespace std) before an include. If you do use using directives, they should come after all your includes. It is also rather strange that you have both a using directive and using declarations for names in that namespace (i.e., std::cout, std::cin, std::endl and std::string).
Right. Is there any specific problem with your program? Like, if it fails to compile, you should state the error messages. If it does not run as expected, you should tell us how does it not work.
-
February 4th, 2009, 03:01 AM
#7
Re: Please help! [Variables and text input]
you need functions and while loops!
goto is a bad habit because it ends up with messy code, which is probably messing up your code.
-
February 4th, 2009, 03:46 AM
#8
Re: Someone please help me out!!
[ Merged threads ]
@To Gabiru: Please use [CODE] tags to make source code more readable!
-
February 4th, 2009, 04:53 AM
#9
Re: Please help! [Variables and text input]
Code:
goto start; //Returns to the beggining of the code.
//...
goto finish; //Goes to the end of the code.
//...
goto back; //Goes back to the prompt for continuance phase.
http://en.wikipedia.org/wiki/Spaghetti_code
Please get rid of the gotos.
Regards,
Paul McKenzie
-
February 4th, 2009, 06:13 AM
#10
Re: Someone please help me out!!
Thanks for replying ;]
Ok, I understand that my code may be "spaghetti"-like, and I'll do my best to fix that, but for me to do so, I'll need some pointers. I assume that when you say: "remove the goto's", you mean :"remove the goto's and code something more efficient to replace them" ... so what can I work with here?
Although the code is using the goto's, I didn't leave out any blank spots.. and it IS a rather simple code, so it bugs me that it's not working. I made a few adjustments, and here is what I was at:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int num1;
int num2;
int num3;
using namespace std;
#include <string>
using std::string;
int main() {
start:
cout << "Please type a number\n" << endl;
cin >> num1;
cout << "Please type another number\n" << endl;
cin >> num2;
num3 = num1 + num2;
cout << "The sum is " << num3 << endl;
back:
cout << "Would you like to continue?\n" << endl;
string answer; //Creates the string named "answer".
getline (cin, answer); //Adds whatever the user typed to the newly created string.
if (answer == "yes") { // Checks if what was typed was "yes".
string answer = ""; //Cleans the string, so that it won't cause an infinite loop.
goto start; //Returns to the beggining of the code.
}
if (answer == "no") { //Checks if the user typed "no".
string answer = ""; //Cleans the string...
}
if ((answer != "yes") && (answer != "no")) {
cout << "Sorry, I didn't understand that.\n";
}
return 0;
}
What happens is that after entering in the first and second numbers, it gives me the result, and then asks me if I want to continue, but nomater what I type(yes/no/haha/huhu...) it executes the display message intended for the condition of the answer string not being yes or no(the last if statement). And lastly, when you do type something, it just ends the code, saying to press any key to continue, so it'll close.
Before I was getting an infinite loop error, and I thought it might have been due to the way I made the string clear, but when I made the last adjustment, taking out the else's and cleaning up the if's, the loop problem got solved.
Ty for the help ;]
-
February 4th, 2009, 09:20 AM
#11
Re: Someone please help me out!!
Originally Posted by Gabiru
Thanks for replying ;]
Ok, I understand that my code may be "spaghetti"-like, and I'll do my best to fix that, but for me to do so, I'll need some pointers. I assume that when you say: "remove the goto's", you mean :"remove the goto's and code something more efficient to replace them" ... so what can I work with here?
It's not a question of efficiency. Since gotos are closer in meaning to what the final assembly code will do after compilation, strictly speaking they might be more efficient than the alternative (very slightly).
It's a question of readability and maintainability. If you see a "goto" statement in a program that you don't know inside and out, you have to search all over to find the label it's jumping to. Very difficult to understand at a glance what is going on.
You should structure your code using loops and if statements entirely rather than gotos. That way, when you come to the end of a loop or even a break or continue statement, it's immediately clear where the code will be going next----either right after the loop block if the loop is over, or to the beginning of it.
-
February 4th, 2009, 09:24 AM
#12
Re: Someone please help me out!!
Originally Posted by Gabiru
Ok, I understand that my code may be "spaghetti"-like, and I'll do my best to fix that, but for me to do so, I'll need some pointers. I assume that when you say: "remove the goto's", you mean :"remove the goto's and code something more efficient to replace them" ... so what can I work with here?
As I suggested, you can use a loop. In conjunction with the loop you can write and use functions as E-man96 suggested, though helper functions are probably not very useful at this point.
Originally Posted by Gabiru
What happens is that after entering in the first and second numbers, it gives me the result, and then asks me if I want to continue, but nomater what I type(yes/no/haha/huhu...) it executes the display message intended for the condition of the answer string not being yes or no(the last if statement).
One possible reason is that this defines a new variable named answer in a more local scope:
Code:
string answer = ""; //Cleans the string, so that it won't cause an infinite loop.
You probably intended to write:
-
February 4th, 2009, 09:53 AM
#13
Re: Someone please help me out!!
Ok, replacing the goto's for while loop. But I think the problem is the string... the program doesn't seem to be injecting the user input into the string variable =(, any ideas?
-
February 4th, 2009, 09:58 AM
#14
Re: Someone please help me out!!
Originally Posted by Gabiru
But I think the problem is the string... the program doesn't seem to be injecting the user input into the string variable =(, any ideas?
As in you applied the fix that I suggested and the problem persists? Time to use a debugger, methinks.
-
February 4th, 2009, 10:18 AM
#15
Re: Someone please help me out!!
This is the getline() problem that's been addressed in about 3 threads in the last few days. Long story short: Try putting cin.ignore(some large number,'\n') in front of the getline().
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
|