C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 10th, 2003, 12:51 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
Looping with amount of letters?

If I type more then one letter/number in the input the, "you must answer with a 1 or a 2" text shows up for the amount of characters/numbers typed in. For example, I type "asd" (without quotes) It will say, "you must answer with a 1 or a 2" three times! Here is my code:

#include <iostream>

int main()
{
using namespace std;


int x = 1;
char y;

do

{
cout << "Is x 1 or 2? (If you type anything else then 1 or 2 the program will repeat until a valid value is entered.\n";
cin >> y;
cout << "\n";

if(y != '1' && y != '2')
cout << "Sorry, you must answer with a \"1\" or a \"2\"!\n\n";
}

while (y != '1' && y != '2');

int int_y;
if(y == '1') int_y = 1;
else int_y = 2;

if (y == '1')
cout << "Correct! X is one\n";
else if (y == '2')
cout << "You are wrong!\n";

return 0;

}

Reply With Quote
  #2  
Old February 10th, 2003, 01:22 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Hi andy,

OK, there's a little hitch in my solution. Here's what's happening: if you enter "asd", cin only reads the first character, so when the loop executes again, the "sd" is still in the input stream, so cin goes ahead and reads the next character without the user doing anything, so that's why you're getting the loop message repeated 3 times.

What you need to do is read in everything the user inputs. There is a way to do that with:

cin.getline();

That function takes two parameters(or more). The first parameter should be the name of a character array, and the second is an integer which is the maximum number of characters you want to read.

so, you can do something like this:

const int maximum=1000;
char answer[maximum]={0};

and then use:

cin.getline(answer, maximum);

to read in data. The function will read in all the characters the user entered before hitting return(\n signals the end of the input) up to a maximum of 1,000 characters.

Then you have to change your if-statement tests and while-loop test to check whether:

answer[0]

is equal to '1' or '2'. Ideally, you could use the code you have, and then clear the input stream to get rid of the extra characters, but I don't know how to do that.

Last edited by 7stud : February 10th, 2003 at 01:32 PM.

Reply With Quote
  #3  
Old February 10th, 2003, 01:35 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
I don't understand what the '={0};' is for in this line:

char answer[maximum]={0};

Reply With Quote
  #4  
Old February 10th, 2003, 02:48 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Hi,

Here is the way you initialize an array:

int numbers[3]={5, 10, 7};


It's good programming style to always intialize your variables to something like 0 when you declare them:

int count=0;
float number=0.0f;

If you only list some of the numbers for an array, the other indexes will automatically be initialized with 0:

int other_numbers[3]={0, 1};

If you printed out that array, you would see that:

other_numbers[0]=0;
other_numbers[1]=1;
other_numbers[2]=0;

So, if you want to intialize a whole array with 0, you only have to intialize the first value with 0, like this:

int more_numbers[100]={0};

instead of:

int more_numbers[100]={0, 0, 0, 0, 0, .....0};

Last edited by 7stud : February 10th, 2003 at 02:58 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Looping with amount of letters?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway