strings and loop

Input a sentence (with the getline(cin, string) command from before) and an
integer value, for the number of time the sentence will print (check for positive number
as in the recommended loop above, but no worry about characters).

Simulate an output as the following:

Enter a sentence to write on the board:
I will not chew gum in class!!!

How many times should it be written? 5

The concatenated string is now:
I will not chew gum in class!!!
I will not chew gum in class!!!
I will not chew gum in class!!!
I will not chew gum in class!!!
I will not chew gum in class!!!

this is what im suppose to code and so far i did that much but its not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
#include <string>
using namespace std;



int main()
{
    
    int num;
    string sentence;
    
    cout<< "Enter a sentence to write on the board.\n";
    getline(cin,sentence);
    
    
    for (int i=0, i<=0,i++);
    cout << "how many times would you like it to be written?";
    cin<<num;
    cout<<num<<endl;
    

return 0;
    
}


for (int i=0, i<=0,i++); This is a syntax error. You should use semi-colons, not commas. And it wouldn't be doing anything useful anyway. It loops 1 time and has no body.
I suggest carefully reading: http://www.cplusplus.com/doc/tutorial/control/ (for loop section)

it's cin >> num;
not cin << num;
Think of the direction of the arrows as being the direction that the information is flowing; it's flowing from cin (your input) to the variable (num).

Once you enter the string and num, you should be looping num times (using a for loop).
Last edited on
Try this
1
2
3
4
5
6
7
    cout << "How many times would you like it to be written? ";
    cin >> num;

    for (int i=0; i <= num; i++)
    {
        cout<<num<<endl;
    }
Topic archived. No new replies allowed.