Home > Software engineering >  How can I save a string input with blank space in c (I am using if else statement)
How can I save a string input with blank space in c (I am using if else statement)

Time:05-04

So I am trying to make a text multiplier , here is the code

#include <iostream>
using namespace std;
int main()
{
    bool m, n;
    string x;
    int y;
    cout << "enter how many times you want to multiply the text : ";
    cin >> y;
    isdigit(y);
    if (y)
    {
        cout << "enter the text you want to multiply : ";
        cin >> x;
        for (int a = 1; a <= y;   a)
            cout << x << endl;
    }
    else
    {
        cout << "you did not entered a number , try again";
    }
    return 0;
}

Everything was fine until I came to know that it was not saving the text input with a blank space I searched how to store string input with blank space and then changed the code but it didn't work. The changed code was

#include <iostream>
using namespace std;
int main()
{
    bool m, n;
    char x[100];
    int y;
    cout << "enter how many times you want to multiply the text : ";
    cin >> y;
    isdigit(y);
    if (y)
    {
        cout << "enter the text you want to multiply : ";
        cin.getline(x, 100);
        for (int a = 1; a <= y;   a)
            cout << x << endl;
    }
    else
    {
        cout << "you did not entered a number , try again";
    }
    return 0;
}

Please help

  • List item

CodePudding user response:

If I understand what you want to do, you need to read the integer value, clear the remaining '\n' that is left in stdin by operator>>, and then use getline() to read the text you want to multiply, e.g.

#include <iostream>
#include <limits>

using namespace std;

int main()
{
    string x;
    int y;
    
    cout << "enter how many times you want to multiply the text : ";
    
    if (!(cin >> y)) {  /* validate stream-state after EVERY input */
      std::cerr << "error: invalid integer input.\n";
      return 1;
    }
    /* clear remaining '\n' from stdin (and any other characters) */
    std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    
    cout << "enter the text you want to multiply : ";
    
    if (!getline (cin, x)) {  /* validate stream state */
      std::cout << "user canceled input.\n";
      return 0;
    }
    
    for (int a = 1; a <= y;   a)
        cout << x << endl;
    
    return 0;
}

Note: the use of isdigit(y) is superfluous. If you validate the input correctly, you determine whether a valid integer was entered at the time of the read simply by checking the stream-state after the read. If failbit is set, the user did not enter a valid integer.

While fine for test code, you will want to look at Why is “using namespace std;” considered bad practice?

Example Use/Output

$ ./bin/multiplytext
enter how many times you want to multiply the text : 3
enter the text you want to multiply : my dog has fleas
my dog has fleas
my dog has fleas
my dog has fleas

If I misinterpreted your goal, let me know and I'm happy to help further.

CodePudding user response:

As seen from this answer, you are mixing the >> operator and getline() which causes syncing issues as getline is not waiting for the input to flush.

You can call either

cin.ignore();

or

cin.clear();
cin.sync();

just before getline().


Patched code:

#include <iostream>
using namespace std;
int main()
{
    bool m, n;
    char x[100];
    int y;
    cout << "enter how many times you want to multiply the text : ";
    cin >> y;
    isdigit(y);
    if (y)
    {
        cout << "enter the text you want to multiply : ";
        cin.ignore();
        cin.getline(x, 100);
        for (int a = 1; a <= y;   a)
            cout << x << endl;
    }
    else
    {
        cout << "you did not entered a number , try again";
    }
    return 0;
}
  •  Tags:  
  • c
  • Related