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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 5th, 2003, 01:02 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
endless loop for some reason

The following basic example is creating an endless loop when I put in anything but 1 or 2, whats going on? I just want it to start the program over again if the inputted value isn't 1 or 2. Thanks in advance.

#include <iostream.h>

void main()

{

bool x;
int cat = 0;

do

{

cout << "Is cat true? 0 for yes, 1 for no.\n\n";
cin >> x;

if (x == 0)
cout << "You are correct!!\n";
else if (x == 1)
cout << "You are not correct \n";
else
cout << "Type in a valid number please.\n";
}

while (x != 0 || x != 1);

}

Reply With Quote
  #2  
Old February 5th, 2003, 01:11 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
Change
while (x != 0 || x != 1);
to
while (x != 0 && x != 1);

Reply With Quote
  #3  
Old February 5th, 2003, 01:56 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
C:\Documents and Settings\Administrator\Desktop\C++ files\options\asdf.cpp(15) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion)
Error executing cl.exe.

Thats what it says for the cin statement. What should I do?

Reply With Quote
  #4  
Old February 5th, 2003, 02:51 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
Change your declaration of x from
bool x;
to
int x;

Reply With Quote
  #5  
Old February 5th, 2003, 03:26 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
It works, but now when I type something like "sdafsda" or any character, it says its correct. How do I get back to saying its not correct if anything but 0 and 1 are typed? Modified code:

#include <iostream.h>

void main()

{

int x;
int cat = 0;

do

{

cout << "Is cat true? 0 for yes, 1 for no.\n\n";
cin >> x;

if (x == 0)
cout << "You are correct!!\n";
else if (x == 1)
cout << "You are not correct \n";
else
cout << "Type in a valid number please.\n";
}

while ((x != 0) && x (!= 1));

}

Reply With Quote
  #6  
Old February 5th, 2003, 11:29 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
Maybe you can change x to type char and alter your if statements to compare characters instead of numbers:
Code:
#include <iostream.h>

void main()
{

char x;
int cat = 0;

do

{

cout << "Is cat true? 0 for yes, 1 for no.\n\n";
cin >> x;

if (x == '0') 
cout << "You are correct!!\n";
else if (x == '1')
cout << "You are not correct \n";
else 
cout << "Type in a valid number please.\n";
}

while ((x != '0') && (x != '1'));
}

Seriously though, if you're just starting to learn C++, you should try learning other concepts before coming to input validation stuff. Also, while you're at it, void main() violates ISO C++ standards (should return int, according to the standard). Check out this link and this link for more details as to why this is so.

Reply With Quote
  #7  
Old February 6th, 2003, 12:42 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
Thanks for your help so far man. But I can't seem to take your suggestions and make my code work. Everything works perfectly unless the user types in a letter. When they type in a letter the program loops endlessly. AHHHH Here is my new code:


#include <iostream>

int main()
{
using namespace std;


int x = 1;
int 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)
cout << "Correct! X is one\n";
else if (y==2)
cout << "You are wrong!\n";
}

while ((y !=1 && y !=2));
return 0;
}

Reply With Quote
  #8  
Old February 6th, 2003, 11:57 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
ok, i understand why that is happening to u, but exactly how to fix it is tricky. i used to have same problem last year in my cs class. input validation is extremely hard to get correct, at least from my experience. what u need, is a loop inside the do loop that checks the input stream to make sure it is valid. when cin expects an integer, and recieves a character, it sets a flag:
cin.rdstate() to 2, the value of cin.rdstate() is 0 normally. so perhaps u could create a loop using cin.rdstate()?? if ur not familiar with that, have u ever used cin.good(). it is ezier to use than the above mentioned, someting like this:

cin >> y;
while(!cin.good())
{
cin.clear();
cin >> y;
}

hope that helps u out

Reply With Quote
  #9  
Old February 9th, 2003, 03:49 AM
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,

I'm not sure why your loop goes crazy when the user enters a letter instead of the expected integer, but you can validate your input very easily if you use a char type for the input from the user because a char literal can be anything:
Code:
int x=1;
char answer;
do
{
	cout<<"Is x equal to 1 or 2?"<<endl;
	cin>>answer;
	if(answer != '1' && answer != '2')
	cout<<"Sorry, you must answer with a \"1\" or a \"2\"!"<<endl;

}while(answer != '1' && answer != '2');

cout<<"Thank you for the proper input.\n";

//Now you can continue with the rest of the program


In the rest of your program, your if statement conditions should test for char literals because the char variable "answer", in the case where the user inputs 1, stores the ASCII code that corresponds to 1 which is 49, so "answer" will not equal 1 but it will equal '1'.

if(answer == '1') cout<<"You are right!";

Alternatively, if for some reason you need an integer as the input, after validating the user's input, you can convert it to an integer like this:

int int_answer=0;
if(answer == '1') int_answer = 1;
else int_answer = 2;

Last edited by 7stud : February 9th, 2003 at 04:33 AM.

Reply With Quote
  #10  
Old February 9th, 2003, 10:17 AM
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
Thanks man. It works! I couldn't figure out the whole cin.good thing like that other guy said. But thanks for the suggestions.
-andy

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > endless loop for some reason


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 |