#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]);
}
problem with while looploop must exit with user input
Page 1 of 1
8 Replies - 1206 Views - Last Post: 20 August 2009 - 11:51 AM
#1
problem with while loop
Posted 20 August 2009 - 02:25 AM
Replies To: problem with while loop
#2
Re: problem with while loop
Posted 20 August 2009 - 02:49 AM
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]);
}
#3
Re: problem with while loop
Posted 20 August 2009 - 03:20 AM
no2pencil, on 20 Aug, 2009 - 01:49 AM, said:
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]);
}
#4
Re: problem with while loop
Posted 20 August 2009 - 03:44 AM
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.
#5
Re: problem with while loop
Posted 20 August 2009 - 05:33 AM
Elcric, on 20 Aug, 2009 - 02:44 AM, said:
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.
#6
Re: problem with while loop
Posted 20 August 2009 - 05:44 AM
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
#7
Re: problem with while loop
Posted 20 August 2009 - 11:43 AM
This post has been edited by Mrafcho001: 20 August 2009 - 11:44 AM
#8
Re: problem with while loop
Posted 20 August 2009 - 11:47 AM
AntonWebsters, on 20 Aug, 2009 - 06:44 AM, said:
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.
#9
Re: problem with while loop
Posted 20 August 2009 - 11:51 AM
Don't worry, I don't it once a year to.
|
|

New Topic/Question
Reply




MultiQuote






|