Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Need help with my C++ Code.... It is a simple program that is supposed to encrypt
Old 09-13-2009, 12:05 PM Need help with my C++ Code.... It is a simple program that is supposed to encrypt
Junior Talker

Posts: 1
Trades: 0
Hello I am having trouble getting a program that I wrote to work correctly. The code worked when I used ,cin >> input, to get the string but I could not use spaces. So I tried getline(cin, input). Well it works but once it is put inside my if statement it wont run correctly. So here is the piece of my code that seems to be messing up.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int ans;
int even;
string output;
cout << "Press 1 for a for loop or press 2 for a while loop: ";
cin >> ans;

if (ans==1) // if answer = 1 then do this code.
{
string input = "";
cout <<"Enter a string: \n";
getline(cin, input); // get string
cout <<"You entered " << input << endl << endl;
}
my else statement is commented out right now.

and then this is my output - it wont let me enter my string but it would when i didn't have the first cout statement and the if statement.

Press 1 for a for loop or press 2 for a while loop: 1
Enter a string:
You entered


Please help

Last edited by HulkThrash; 09-13-2009 at 02:44 PM..
HulkThrash is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-22-2009, 10:08 AM Re: Need help with my C++ Code.... It is a simple program that is supposed to encrypt
helloworlder's Avatar
Novice Talker

Posts: 10
Trades: 0
Hello, add the line in blue and please tell me if it fixes your problem. I think the problem is there's still a new line character in your input buffer, and cin.ignore() gets rid of it. Check this link:

http://www.cplusplus.com/forum/beginner/9148/

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int ans;
int even;
string output;
cout << "Press 1 for a for loop or press 2 for a while loop: ";
cin >> ans;
cin.ignore();

if (ans==1) // if answer = 1 then do this code.
{
string input = "";
cout <<"Enter a string: \n";
getline(cin, input); // get string
cout <<"You entered " << input << endl << endl;
}

system("PAUSE");
}
__________________

Please login or register to view this content. Registration is FREE
- Learn Chinese through bite sized blocks

Please login or register to view this content. Registration is FREE
- Land the Plane

Please login or register to view this content. Registration is FREE
- Blog
helloworlder is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help with my C++ Code.... It is a simple program that is supposed to encrypt
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.56626 seconds with 12 queries