GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Oct-2008, 14:42
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Whats wrong? Using a function in strings.


I'm trying to do add characters to my string: you can see as follows:

CPP / C++ / C Code:

//My working prototype

void AddToString(string& str, char ch, int num);




//Part of my main Program

else if (choice == 3){
			string str;
			char ch;
			int num=0;
			cout << "Enter a string" << endl;
			cin >> str;
			cout << "Enter a character"<< endl;
			cin >> ch;
			cout << "How many times would you like to add the character?" << endl;
			cin >> num;
			//cout << "REPLACE this stmt with a call to AddToString" << endl;
			AddToString( str, ch, num );      
      //cout << "REPLACE this stmt by print the new string with a message"<<endl;
      cout << "The New String: " << str << endl;



// My Function Definition

void AddToString(string& str, char ch, int num){
	
	int loop=0;

	
  while ( loop < num)
	
	      str = str + ch;
	      
        loop++;
	      
		
}



Any help is appreciated why it isn't working properly. After the program ask me to input how many times to add the character, it just stops and sits as it is.

Thanks.
  #2  
Old 16-Oct-2008, 16:19
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Whats wrong? Using a function in strings.


When using loops, you need to enclose the statements in a set of brackets if you have multiple statements, otherwise the compiler assumes you are looping only the first statement.

CPP / C++ / C Code:
while( loop < num )
{
  str = str + ch;
  loop++;
}
  #3  
Old 16-Oct-2008, 16:46
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: Whats wrong? Using a function in strings.


Quote:
Originally Posted by fakepoo
When using loops, you need to enclose the statements in a set of brackets if you have multiple statements, otherwise the compiler assumes you are looping only the first statement.

CPP / C++ / C Code:
while( loop < num )
{
  str = str + ch;
  loop++;
}

Wow..What a dumb mistake!

Works perfect! Thanks so much.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 12:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:56.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.