0

I am struggling to create a loop for getting input from user. The input must push_back() each instance.

#include <iostream>
#include <array>
#include <cstring>
#include <vector>
#include <string>
#include <string.h>
using namespace std; 

int main()
{
    vector <string> bookQ = { "what","book","is","that","you","are","reading" };
    for (int i = 0; i < bookQ.size(); i++) {
        cout << bookQ[i] << ' ';        
    }
    cout << endl;
    string input;
    int x = 0;    
    for (x != '1') {                                 // require a loop to input string and  end when user prompts                      
        cout << "Enter 1 to stop" << endl;           //
        cin >> x;                                    //
        getline(cin, input);                         //
        bookQ.push_back(input);                      //
    }                                                //
    for (int i = 0; i < bookQ.size(); i++) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Hendrik
  • 13
  • 1
  • whats wrong with the code? Please include example input, output and expected output in the question – 463035818_is_not_an_ai Jul 03 '22 at 19:45
  • 3
    On your input loop you're using `for (x != '1')`. That should probably be a `while` instead of `for`. – jkb Jul 03 '22 at 19:49
  • Error occurs at the loop // (x != '1') " ! <- "not declared and states ";" needs to be added – Hendrik Jul 03 '22 at 19:49
  • That's exactly the problem @jkb mentioned. – Ted Lyngmo Jul 03 '22 at 19:53
  • Problem with while loop: Output just runs after any input without end... want the user to input eg. "my book c++" but as "my" then "book" then "c++" and user finished then ends loop – Hendrik Jul 03 '22 at 19:54
  • @Hendrik Did any of the answers help? If you haven't read it, this is good: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Ted Lyngmo Jul 08 '22 at 00:16

4 Answers4

2

Your for loop is missing the declaration and (iteration) expression parts:

for (declaration-or-expression; declaration-or-expression; expression)

so it should have looked like this:

for (;x != '1';) {

which is generally written as

while (x != '1') {
  • That would cause problems though since it would not stop directly when the user entered 1.
  • You are also comparing an int with a char ('1'), so in order to exit the loop, the user would have had to enter 49 (the ASCII value for 1), not 1.
  • You are also mixing formatted input (cin >> x) with unformatted input (getline). I suggest that you stick to one only.

Example:

while(cout << "Enter 1 to stop\n", getline(cin, input) && input != "1") {
    bookQ.push_back(input);
}                                                
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

simply check if input is 1 everytime the user enters somthing, and when it does = 1, simply break loop.

string x;
while (true) {                                 // require a loop to input string and  end when user prompts                      
    cout << "Enter 1 to stop" << endl;
    cin >> x;
    if (x == "1"){
       break;
    }
    getline(cin, x);
    bookQ.push_back(x);
}
}    
Hannon qaoud
  • 785
  • 2
  • 21
0

Assuming you meant that input is a string, then you've made a few mistakes with types. First of all, you've used wrong type for variable x, you used int which is integer type, and the type string is required. Secondly, when comparing x with '1' you used single quotes, which define the type of variable as char, not string. To make 1 a string you should use double quotes, like so "1". Besides that, you have used for(condition), which is incorrect syntax. You should use while(condition). Also, when your loop iterates, the x variable is the input book name, and input variable is always an empty string, so I would suggest replace input with x everywhere. The working code is below.

P.S. I am not sure whether you want "1" to be in the final vector, so I haven't changed that

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> bookQ = {"what", "book", "is", "that", "you", "are", "reading"};
    for (int i = 0; i < bookQ.size(); i++) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;

    string input;
    string x;
    while (x != "1") {
        cout << "Enter 1 to stop" << endl;
        cin >> x;

        bookQ.push_back(x);
    }

    for (int i = 0; i < bookQ.size(); i++) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;

    return 0;
}
knyaz
  • 1
  • 1
  • This is better than my answer, unfortunately it adds the final `"1"` to the end of the vector. – john Jul 03 '22 at 20:08
0

First, your for syntax is wrong. You want a while loop instead, or in this case a do..while loop would make more sense. Also, you are pushing the user's input into the vector before validating what the input actually is.

Second, x is an integer, but '1' is a character whose ASCII value is number 49. Your loop will never end, because != will always be true. Since you want the user to enter number 1 to stop the loop, you need to drop the quotes:

Third, what is the point of pre-populating bookQ? Just declare the bookQ without any initial data, and then cout the entire question as a normal string. This way, after the user is done entering input, the vector will contain only the user's input and nothing else.

Try something more like this:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std; 

int main()
{
    vector <string> bookQ;
    string input;

    cout << "what book is that you are reading" << endl;

    do {
        cout << "Enter a book, or 1 to stop" << endl;
        getline(cin >> ws, input);
        if (input == "1") break;
        bookQ.push_back(input);
    }
    while (true);

    for (size_t i = 0; i < bookQ.size(); ++i) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770