ambiguous if-else loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenneth6
    New Member
    • Mar 2007
    • 79

    ambiguous if-else loop

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char firstname;
    	char finalFirstname[20];
    
    	cout << "Enter your first name: ";
    	int num = 0;
    	int check = 0;
    
    	while(firstname = cin.get())
    	{
    		if(firstname == '\n')
    			break;
    		if(firstname == ' ')
    		//{
    		//	if(check == 0)
    		//	{
    		//		if(num != 0)
    		//		{
    		//			finalFirstname[num] = '-';
    		//			num++;
    		//			check = 1;
    		//		}
    		//	}
    		//}
    		//else
    			continue;
    		finalFirstname[num] = firstname;
    		num++;
    	}
    
    	for(int j=0; j<num; j++)
    	{
    		cout << finalFirstname[j];
    	}
    	cout << endl;
    
    	return 0;
    }
    The program allows user to input first name.
    1) if there is a space between the 2 words, a hyphen is replaced.
    2) if there is spaces in front of or after surnames, try all them.

    the 2) was done by running the above script.
    the lines commented are used to replace the space between 2 character (not space) with hyphen, but it fails to run. Does the problem lie in the ambiguous else? else should follow the 2nd if-loop.

    Pls help!!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    First, the if statement is not a loop. Only do...while, while, and for statements are loops. The if statement is called a selection statement, along with else and switch.

    Now, why are you using the continue statement? This doesn't do what you think it does - check out the documentation here.

    Comment

    Working...