![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
breaking if
hello see this code...
#include <iostream>
#include <string>
using namespace std;
int main()
{
string type,nick;
int count;/////////////main menu
cout<<"Welcome to the nickname case changer!!!"<<endl
<<"Please enter \"up\" to make the nick name all caps."<<endl
<<"Please enter \"down\" to make the name all lower case."<<endl
<<"Please enter \"du\" to make it look LiKe ThIs."<<endl
<<"Please enter \"ud\" to make it look lIkE tHiS.(Lower case first)"<<endl;
getline(cin,type);
if(type=="up")
{
cout<<"\n\aPlease enter your nickname."<<endl;
getline(cin,nick);//asks for nickname
for(count=0;count<=nick.length();count++)
nick[count]=toupper(nick[count]);//this loop changes all the nick name to upper case.
cout<<nick<<endl<<endl;
return 0;
}
else if(type=="down")
{
nick="";
count=0;
cout<<"\nPlease enter your nickname."<<endl;
getline(cin,nick);//asks for nickname
for (count = 0; count <= nick.length(); count++)
nick[count] = tolower(nick[count]);//this loop changes all the nick name to lower case.
cout<<nick<<endl;
return 0;
}
}The problem is when i enter the word down it just continues with the word first if. is there any way u can break the 'if' or something. thank you for helping!!
__________________
From the bottom of the stone steps... ...i'm calling still. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string type, nick;
int count;
cout << "Welcome to the nickname case changer!!!" << endl
<< "Please enter \"up\" to make the nick name all caps." << endl
<< "Please enter \"down\" to make the name all lower case." << endl
<< "Please enter \"du\" to make it look LiKe ThIs." << endl
<< "Please enter \"ud\" to make it look lIkE tHiS.(Lower case first)" << endl;
cin >> type;
cout << "\n\aPlease enter your nickname." << endl;
cin >> nick;
if( type == "up" )
for( count = 0; count <= nick.length(); count++ )
nick[ count ] = toupper( nick[ count ] );
else if( type == "down" )
for ( count = 0; count <= nick.length(); count++ )
nick[ count ] = tolower( nick[ count ] );
else nick = "'up' or 'down'! you entered " + type;
cout << nick << endl << endl;
cin >> nick; // pause to view!
return 0;
}I like to stay away from getline()... it has some non-intuitive behaviour (at least with VC++ and IMHO ) I also like to be generous with whitespaces to make the code a little less agonizing on my poor old eyes. Two more things.. 1) Don't forget to return an int when type is neither "up" nor "down", and 2) Try to avoid two or more occurences of the exact same code if possible (and sensible!).
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Jul 31st, 2005 at 6:24 AM. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Getline seems to be buffering the input and accepting two values. To observe this, start your version of the program, and type in the following when prompted to enter the type (<enter> is the enter button):
down<enter> cAtSNdOgS<enter> and when prompted for the nick: thiswillnotbeseen<entered> you might want to add "cin >> nick;" at the end so you can see what is happening (or step through with your debugger, watching the values of nick and type) That will do tolower on catsndogs, demonstrating what I mean. There is likely some kind of "clearbuffer" command or similar but I don't have the time to research right now...
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string type, nick;
int count;
cout << "Welcome to the nickname case changer!!!" << endl
<< "Please enter \"up\" to make the nick name all caps." << endl
<< "Please enter \"down\" to make the name all lower case." << endl
<< "Please enter \"du\" to make it look LiKe ThIs." << endl
<< "Please enter \"ud\" to make it look lIkE tHiS.(Lower case first)" << endl;
cin >> type;
// Was an acceptable type entered? ( extend for du and ud )
while(( type != "up" ) && ( type != "down" ))
{
cout << You entered " << type << " . Please enter 'up' or 'down'.\n";
cin >> type;
}
cout << "\n\aPlease enter your nickname." << endl;
cin >> nick;
if( type == "up" )
for( count = 0; count <= nick.length(); count++ )
nick[ count ] = toupper( nick[ count ] );
else
for ( count = 0; count <= nick.length(); count++ )
nick[ count ] = tolower( nick[ count ] );
cout << nick << endl << endl;
cin >> nick; // pause to view!
return 0;
}This is more natural. I was accepting the nick even if a bad type was entered.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Thanx dude
![]()
__________________
From the bottom of the stone steps... ...i'm calling still. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|