problem with while loop

loop must exit with user input

Page 1 of 1

8 Replies - 1494 Views - Last Post: 20 August 2009 - 11:51 AM Rate Topic: -----

#1 bhuelsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-August 09

problem with while loop

Posted 20 August 2009 - 02:25 AM

Here is the problem I'm having. Below is my code and yes it is for a hw assignment. It is actually a failry easy program and for the life of me can't figure out what I'm doing wrong with my while loop. Basically its the last assigment right before out final so the instructor told us just to write a simple program that takes a string from the user and converts all the lower case to upper case. Well that part work just fine. The second part of the assignment was that we we need to use a while loop that will exit when the user inputs Q or q. And for some reason this is the part that is giving me trouble. For one I can not get it to exit on with the Q or q and for some other reason it's showing all my output statements if the user inputs something other then q or either Q or q. Then it will let them input a string and it will just show the result. Its kind of confusing to explain but hopefully someone gets what I mean. [/size]








#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
char quit='q';
void cStringToUpper(char *s1);

char s1[11];
int main()
{
while (quit !='q' || quit != 'Q')								
{
cout<<"Enter your string to be converted without any spaces:"<<endl;		
cin.getline(s1,10);															stores it into s1

cStringToUpper(s1);														to uppercase
cout<<"The converted string is:"<<endl;
cout<<s1;					
cout<<endl;

cout<<"Press (q or Q) to quit:"<<endl;		
cin>>quit;


}		
cin.ignore(2);
return 0;

}	

void cStringToUpper(char *s1)				
{
int i;
int length = strlen(s1);			
for (i=0;i< length; i++)		
s1[i]= toupper(s1[i]);		
} 


Is This A Good Question/Topic? 0
  • +

Replies To: problem with while loop

#2 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5166
  • View blog
  • Posts: 26,849
  • Joined: 10-May 07

Re: problem with while loop

Posted 20 August 2009 - 02:49 AM

Since you already have included ctype, I suggest the following

First, if you define quit to already equal the letter 'q', then how do you expect to enter the while loop if the condition is while the variable quit is not equal to the letter 'q'? So we take the or logic out of your while statement, & just check the upper case value of quit. That's all that I altered, & it works just like you described it.

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
char quit='\0';
void cStringToUpper(char *s1);

char s1[11];
int main(void) {
  while(toupper(quit)!='Q') {
	cout<<"Enter your string to be converted without any spaces:"<<endl;
	cin.getline(s1,10);
	cStringToUpper(s1);
	cout<<"The converted string is:"<<endl;
	cout<<s1;
	cout<<endl;

	cout<<"Press (q or Q) to quit:"<<endl;
	cin>>quit;
  }
  cin.ignore(2);
  return 0;
}

void cStringToUpper(char *s1) {
  int i;
  int length = strlen(s1);
  for (i=0;i< length; i++)
  s1[i]= toupper(s1[i]);
}


Was This Post Helpful? 0
  • +
  • -

#3 bhuelsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-August 09

Re: problem with while loop

Posted 20 August 2009 - 03:20 AM

Cool thanks I get what you're saying with the quiting the loop part. That part now works but what about the part that when the loop continues it prints all the cout statements to the screen? You have any idea why it's doing that?


View Postno2pencil, on 20 Aug, 2009 - 01:49 AM, said:

Since you already have included ctype, I suggest the following

First, if you define quit to already equal the letter 'q', then how do you expect to enter the while loop if the condition is while the variable quit is not equal to the letter 'q'? So we take the or logic out of your while statement, & just check the upper case value of quit. That's all that I altered, & it works just like you described it.

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
char quit='\0';
void cStringToUpper(char *s1);

char s1[11];
int main(void) {
  while(toupper(quit)!='Q') {
	cout<<"Enter your string to be converted without any spaces:"<<endl;
	cin.getline(s1,10);
	cStringToUpper(s1);
	cout<<"The converted string is:"<<endl;
	cout<<s1;
	cout<<endl;

	cout<<"Press (q or Q) to quit:"<<endl;
	cin>>quit;
  }
  cin.ignore(2);
  return 0;
}

void cStringToUpper(char *s1) {
  int i;
  int length = strlen(s1);
  for (i=0;i< length; i++)
  s1[i]= toupper(s1[i]);
}


Was This Post Helpful? 0
  • +
  • -

#4 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: problem with while loop

Posted 20 August 2009 - 03:44 AM

Hello, :D

I made a few very picky changes that were not really necessary.

The only problem you had was your while statement. It should have been && instead of ||.

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

void cStringToUpper( char *s1 );

int main( int argc, char* argv[] )
{
	char quit = ' ';
	char s1[ 11 ];

	while ( quit != 'q' && quit != 'Q' )								
	{
		cout << endl << endl << endl;
		cout << "  Enter your string to be converted without any spaces" << endl;
		cout << "  then press Enter  ==> ";		
		cin.getline( s1, 10 );															//stores it into s1

		cStringToUpper(s1);															   //to uppercase

		cout << endl << endl;
		cout << "  The converted string is:" << endl << "  ";

		cout << endl << endl << "  ";
		cout << s1 << endl; 

		cout << endl << endl << "  ";
		system("PAUSE");

		cout << endl << endl;
		cout << "  Press ( q or Q ) to quit then press Enter  ==> ";		
		cin >> quit;
		cout << endl << endl;
	} 

	return EXIT_SUCCESS;
}	

void cStringToUpper( char *s1 )				
{
	int i;
	int length = strlen( s1 );			
	for ( i = 0; i < length; i++ )		
	s1[ i ]= toupper( s1[ i ]);		
}

//Your program would not compile.  It had 14 errors.
//You wrote comments but did not use the // for the comments.
//I added the // for the comments and the 14 errors went away.


Was This Post Helpful? 0
  • +
  • -

#5 bhuelsman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-August 09

Re: problem with while loop

Posted 20 August 2009 - 05:33 AM

Why would you want &&? If you put that it will keep looping when they enter just q or just Q. For && to work quit would have to equal both q and Q. The || (or) lets it equal either one to get a true so that the loop will end.

View PostElcric, on 20 Aug, 2009 - 02:44 AM, said:

Hello, :D

I made a few very picky changes that were not really necessary.

The only problem you had was your while statement. It should have been && instead of ||.

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

void cStringToUpper( char *s1 );

int main( int argc, char* argv[] )
{
	char quit = ' ';
	char s1[ 11 ];

	while ( quit != 'q' && quit != 'Q' )								
	{
		cout << endl << endl << endl;
		cout << "  Enter your string to be converted without any spaces" << endl;
		cout << "  then press Enter  ==> ";		
		cin.getline( s1, 10 );															//stores it into s1

		cStringToUpper(s1);															   //to uppercase

		cout << endl << endl;
		cout << "  The converted string is:" << endl << "  ";

		cout << endl << endl << "  ";
		cout << s1 << endl; 

		cout << endl << endl << "  ";
		system("PAUSE");

		cout << endl << endl;
		cout << "  Press ( q or Q ) to quit then press Enter  ==> ";		
		cin >> quit;
		cout << endl << endl;
	} 

	return EXIT_SUCCESS;
}	

void cStringToUpper( char *s1 )				
{
	int i;
	int length = strlen( s1 );			
	for ( i = 0; i < length; i++ )		
	s1[ i ]= toupper( s1[ i ]);		
}

//Your program would not compile.  It had 14 errors.
//You wrote comments but did not use the // for the comments.
//I added the // for the comments and the 14 errors went away.


Was This Post Helpful? 0
  • +
  • -

#6 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: problem with while loop

Posted 20 August 2009 - 05:44 AM

Let's me try to convert this into English, maybe it'll be easier to understand it.

While quit is NOT 'q' AND NOT 'Q', the loop goes on.

That means, other than entering 'q' AND 'Q', any other kinds of input will make the while loop go on.
Can you understand what I'm trying to explain? =)

This post has been edited by AntonWebsters: 20 August 2009 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: problem with while loop

Posted 20 August 2009 - 11:43 AM

nvm

This post has been edited by Mrafcho001: 20 August 2009 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5166
  • View blog
  • Posts: 26,849
  • Joined: 10-May 07

Re: problem with while loop

Posted 20 August 2009 - 11:47 AM

View PostAntonWebsters, on 20 Aug, 2009 - 06:44 AM, said:

While quit is NOT 'q' AND NOT 'Q', the loop goes on.

At what point will the variable quit equal both lowercase q and upper case q? The loop would run forever.

If it's lower case q, then the 2nd and logic fails. If it's upper case q, then the 1st logic fails.
Was This Post Helpful? 0
  • +
  • -

#9 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Re: problem with while loop

Posted 20 August 2009 - 11:51 AM

HA! That's the funniest thing I've seen all day. haha
Don't worry, I don't it once a year to. :blink:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1