Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 31st, 2005, 4:54 AM   #1
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
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.
darkone916 is offline   Reply With Quote
Old Jul 31st, 2005, 5:58 AM   #2
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
#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.
stevengs is offline   Reply With Quote
Old Jul 31st, 2005, 6:30 AM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
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
stevengs is offline   Reply With Quote
Old Jul 31st, 2005, 6:43 AM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
#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
stevengs is offline   Reply With Quote
Old Jul 31st, 2005, 7:15 AM   #5
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
Thanx dude
__________________
From the bottom of the stone steps...
...i'm calling still.
darkone916 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC