-1

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
sarthakt
  • 42
  • 1
  • 7
omkar kharat
  • 3
  • 1
  • 5
  • Do you mean to use `if (isdigit(y)) { ... }` for your test? Simply calling `isdigit(y)` discards the return. `std::cin` will ignore leading whitespace and stop at the first occurrence of whitespace after non-whitespace has been read. Using `getline()` can preserve whitespace. – David C. Rankin May 04 '22 at 04:21
  • > changed the code but it didn't work You should update the behavior what you expect the code to do, and the behavior what the code actually do. – ramsay May 04 '22 at 04:26
  • @DavidC.Rankin it is not `cin` that ignores whitespace. It is `operator>>` that does. – Remy Lebeau May 04 '22 at 04:44
  • Related/dupe: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/) – Remy Lebeau May 04 '22 at 04:47

2 Answers2

1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I started learning c++ 5 days ago . i got a question that does cerr and cout means the same? – omkar kharat May 04 '22 at 05:54
  • You have 3 standard streams. Stream No. `0` is `stdin` (input), Stream No. `1` is `stdout` normal output and Stream No. `2` is `stderr` where error output is written. `std::cout` writes to `stdout` and `std::cerr` writes to `stderr` (so I'm just writing the error messages on the correct error stream instead of to the output stream). Both will appear on the terminal, but if your redirect program output (e.g. `./program > somefile`) only `stdout` is written to the file and `stderr` is still shown on the terminal. – David C. Rankin May 04 '22 at 06:00
  • If you are just starting to learn C++, bookmark [cppreference.com](https://en.cppreference.com/) and use it for everything. No finer reference on the net. (it takes a while to figure out where everything is on the site -- but well worth it). Also learning C++ isn't a race, it's more of a journey, so enjoy the adventure. It's not something you will learn in a semester, or a year (or forever for that matter, they keep adding to the standard). So just pay attention to the details, be curious, practice, and it will all come in time. – David C. Rankin May 04 '22 at 06:06
  • For example [std::basic_istream::ignore](https://en.cppreference.com/w/cpp/io/basic_istream/ignore) explains in detail what `.ignore()` does. – David C. Rankin May 04 '22 at 06:08
  • if input is wrong , then how can i restart the program without closing it – omkar kharat May 11 '22 at 09:29
  • @omkarkharat - you loop continually until valid input is received. [Example of Requiring Valid Input](https://stackoverflow.com/a/55986486/3422102) – David C. Rankin May 11 '22 at 11:36
0

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;
}
jloh
  • 291
  • 2
  • 8
  • 1
    May want to explain what `isdigit(y);` does (as used) and what `if (y)` is actually checking for. Also note that unless `failbit` is set `.clear()` does nothing and `.sync()` isn't used to clear the input buffer -- there is no guarantee that it does anything to it See: [std::basic_istream::sync](https://en.cppreference.com/w/cpp/io/basic_istream/sync) – David C. Rankin May 04 '22 at 06:09