Thread: how to loop a user difined phrase?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    how to loop a user difined phrase?

    im asking the user to enter a phrase. the user enters "hi" and the loop works depending on the number the user tells it to loop. so far so good but the output is wrong. instead of outputing
    Code:
    hi
    hi
    the prog only outputs the 1st letter of the phrase so it outputs only
    Code:
    h
    h
    i guess the problem is with how the char keyword works or something. anyone any ideas on how to loop a whle sentence/phrase?
    When no one helps you out. Call google();

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Show some code.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    nm, i figured it out. char works as an array so where b4 i had
    Code:
    char theChar;
    i changed it to
    Code:
    char theChar[40];
    so now it works but its limited to 40 letters, is there any way to make it so its not restricted?
    When no one helps you out. Call google();

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    lol ok i still need help. if the user types in "hi how are you" the prog will only output "hi" and the rest will be ommited.. how can i make it so it accepts a whole sentence?
    When no one helps you out. Call google();

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    this is the code
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	//int counter = 0;
    	int num;
    	char theChar[40];
    	cout << "Enter the number you want to loop"<<endl;
    	cin >> num;
    	cout<< "Enter the phrase you want to loop"<<endl;
    	cin >> theChar;
    	
    	for (int i=0; i<num; i++)
    	{
    	cout <<theChar<<endl;
    	}
    	cout<<"\nLoop executed: "<<num<<" time(s).\n";
    	cout <<"Do you really think that "<<theChar<<" is true?\n";
    	return 0;
    }
    When no one helps you out. Call google();

  6. #6
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Take a look at this..maybe it will help ya. Type in as much as you want when it ask for name

    Code:
    #include<iostream>
    #include<string>
    
    	using namespace std;
    
    	int main()
    	{
    		string s;
    		cout << "Please enter your first name followed by a newline\n";
    		cin >> s;
    		cout << "Hello, " << s << '\n';
    		return 0; // this return statement isn't necessary
    	}

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    big146's code solves the limit of 40 characters that InvariantLoop's code has.

    However, it does have the same problem in that it will only get "hi" and stop at the first space.

    To fix both, you would want to use getline which gets an entire line instead of >> which only reads to the next whitespace. One problem with mixing >> and getline (which you would be doing since InvariantLoop's code gets an integer first) is that the >> leaves a newline in the input stream, so the getline will stop there if you don't ignore that newline.

    Try this:
    Code:
    #include <iostream>
    #include <string>
    #include <limits>
    using namespace std;
    
    int main()
    {
        //int counter = 0;
        int num;
        string theString;
        cout << "Enter the number you want to loop"<<endl;
        cin >> num;
        cin.ignore(numeric_limits<int>::max(), '\n');
        cout<< "Enter the phrase you want to loop"<<endl;
        getline(cin, theString);
        
        for (int i=0; i<num; i++)
        {
            cout <<theString<<endl;
        }
        cout<<"\nLoop executed: "<<num<<" time(s).\n";
        cout <<"Do you really think that "<<theString<<" is true?\n";
        return 0;
    }
    Note, you can also use a form of getline that works with char[40]. That getline is a member of cin, so you would use:
    Code:
    cin.getline(theChar, 40);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  4. user input loop
    By asudave2302 in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2004, 09:45 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM