-1

I need to be able to enter a sentence and have it spit back out. However, if a number is entered in the sentence, it should be output along with the words but it must be doubled.

I've tried implementing an if statement into my code to see if I could check if a number was entered, and if so print out that value in the stream *2, however this doesn't work because if I enter a number first then some text it breaks, if I don't enter the number as the second value of the sentence than it only prints the first word entered.

#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>

using namespace std;

int main()
{
    string sentence;
    string word;
    float val=0;
    cout<<"Enter a sentence: ";

    getline(cin, sentence);
    stringstream ss;
    ss.str(sentence);

    while (ss >> word || ss >> val)
    {
        if (ss >> val)
        {
            cout << val * 2;
        }
        else
        {
        cout << word << endl;
        }
    }
    return 0;
}

If I enter a sentence like "I walked 2 times today" then it should output it as:

I 
walked
4 
times today

But this would only be output as:

I
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • 2
    Remember the golden rule of computer programming: a computer will always do exactly what you tell it to do, instead of what you want it to do. If you were to translate the above simple C++ code into English words, what would it be, and does it sound like what you want your computer to do? Remember that if the formatted extraction operator fails, the stream is in a failed state and nothing will happen unless the stream status gets explicitly cleared. That's just one of the numerous problems here. – Sam Varshavchik Aug 21 '19 at 01:08
  • 1
    This is not a bad question: you have some code, a clear problem, expected result and current result, but you could definitely improve your question a lot if you [edit]ed it to contain a [mcve]: that is, the minimum but full amount of code to compile and reproduce your issue, which would aid people a lot in helping – Tas Aug 21 '19 at 01:24
  • Thank you Tas, I edited my code to include the rest of the code. – Luke Duggan Aug 21 '19 at 01:27
  • Sam, you make a very good point about translating the code to english, it's my first time using StringStream so I am going blind into this, I'll attempt to understand the advice you are giving, thank you. – Luke Duggan Aug 21 '19 at 01:28
  • I just resolved the issue! – Luke Duggan Aug 21 '19 at 01:34

1 Answers1

-3

Solved the issue, had to use ss.eof() method to go to the end of the StreamString and stop and if a value that was entered into the string was a number, it would be printed out. My program was stopping when a certain if statement was met.

  • 4
    Why is this an answer? Code-only answers are typically low quality since they are rarely understood by others with the same question. – JaMiT Aug 21 '19 at 03:26
  • 1
    Hm. ````using namespace std;```` and ````while (!ss.eof())```` and ````<< endl```` and ````stringstream ss; ss.str(sentence);````. Maybe this answer should be edited . . . – A M Aug 21 '19 at 07:30
  • I updated the answer now cause you're right. Sorry I'm not very fresh on stackover flow. – Luke Duggan Aug 21 '19 at 23:18