0

The verifier will enter a string of numbers separated by spaces (example: "1 2 45 60 98"). The number of numbers in a string is unknown. You must write each number as a separate element of the array. When compiling, Visual Studio throws an error and reports no exceptions. As i understand, "while" don't stop and continue working. How can I do this task otherwise?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int i = 0;
    int* b = new int;
    while (b[i] != '\n')
    {
        cin >> b[i];
        cout << b[i]; \\ just checking
        i++;
    }
    return 0;
}
shuNya19
  • 5
  • 1

2 Answers2

5

This exhibits multiple varieties of undefined behavior. This should be handled with ::std::vector and ::std::string. You should largely never be using new directly in any code you write.

First, you allocate an int but never initialize it. Then you test it to see if it's value is '\n' (which happens to be 10), but accessing the integer before it's initialized is undefined behavior.

Next, you read in an integer, and then increment i to 1, then access b[i], which is again undefined behavior because you're reading memory that you've never allocated. You allocated space for one int, and you're trying to access a second.

The fact your are confused about why this code doesn't work tells me that you have some really significant misunderstandings about a number of different things.

For example, even without the undefined behavior problems I just mentioned, your loop test would never test the most recently read value, it always tests a value that you haven't yet read.

And also, if you enter a blank line, the read won't even return. That's whitespace, and whitespace is simply skipped over by the iostream library when you ask for an int. Even if you were checking the most recently read value in your while loop, you would have to enter 10 to get your program to stop, not a blank line.

My suggestion would be to find a TA to help you. The number of misunderstandings and mistakes in this simple snippet of code is too hard to unwind in a StackOverflow question without simply telling you what the answer is. And this looks a lot like homework. I'm not going to tell you what the answer is.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • Thank you very much, now i understand my mistakes Will try to correct my code. Maybe you are right about TA, because i'm only starting learning C++. But, honestly, i hope i can understand this language by myself using method of making mistakes. – shuNya19 Oct 29 '19 at 17:38
  • @MaximKardackow , if possible supplement your education with a few other resources. [Here is a curated list of references](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Since books ain't cheap and you probably already have some focused on the art of programming, if not on good use of C++, [here is a link to an abbreviated and terse book posted online by the author](https://isocpp.org/tour) that you can fact-check against. – user4581301 Oct 29 '19 at 17:51
1

read the entire line as a string.

read each int from that string

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <sstream>

using namespace std;

int main()
{
    string line;
    getline(cin, line);
    stringstream s(line);
    vector<int> v;
    copy(istream_iterator<int>(s), istream_iterator<int>(), back_insert_iterator<vector<int>>(v));

    for (auto i:v) {
        cout << i << endl;
    }

    return 0;
}
Victor Padureanu
  • 604
  • 1
  • 7
  • 12
  • 2
    Also, nobody should be using `strok` in a C++ program. Using the built in regular expression library would be better. Also, giving the OP the answer to his question isn't helping him. – Omnifarious Oct 29 '19 at 17:19
  • there is a strtok_s that is context safe that can be used instead of strtok – Victor Padureanu Oct 29 '19 at 17:22
  • It's not whether or not it's context safe that's the problem. The problem is that it's not really C++. I actually think the C++ standard should just tell people to not use the C standard library at all, except for the functions that are basically thin system call wrappers. – Omnifarious Oct 29 '19 at 17:27
  • The variant of `getline` being used here needs extra code to handle potential overflows. `getline` promises the buffer won't be overflowed, but the stream is placed in the fail state to force the programmer to acknowledge and deal with the left-over data. `std::string` and `std::getline` would be a much better choice anywhere `std::vector` would be in use. – user4581301 Oct 29 '19 at 17:34
  • you are right, the proper way would be to use std::getline and put it in a string, and yes, stringstream is a good option. I will update the answer with these details. Thank you – Victor Padureanu Oct 29 '19 at 17:54