0

So, until now I've read the string from keyboard using cin.get or cin.getline and then using a pointer to modifying every word like so:

p=strtok(string,' ');

but I've learned that I can read word by word such as:

char word[101];
cin>>word;

I am curious if I can find a stopping condition if I have an unknown number of words I've tried using:

while(cin>>word)
{
   //condition
}

#include <iostream>
#include <cstring>
using namespace std;
char cuv[101],maxim[101]="nu exista",ok; 
int main() 
{
   while(cin>>cuv) { 
        if(cuv[0]<=57 && cuv[0]>=48)
            if(cuv[0]>maxim[0] || ok==0) { 
                ok=1; strcpy(maxim,cuv); 
            }
     }
      cout<<maxim;
      return 0;
 }
pm100
  • 48,078
  • 23
  • 82
  • 145
Dr8gos
  • 1
  • 3
  • 2
    `while(cin>>word)` should be a good start. What is the problem you had when you used that? – drescherjm Apr 03 '22 at 17:59
  • `char word[101];` you should not use this in `c++`. This is very error prone. Instead use `std::string word;` – drescherjm Apr 03 '22 at 18:00
  • @drescherjm it reads endlessly – Dr8gos Apr 03 '22 at 18:01
  • and I am using :"using namespace std;" – Dr8gos Apr 03 '22 at 18:02
  • #include #include using namespace std; char cuv[101],maxim[101]="nu exista",ok; int main() { while(cin>>cuv) { if(cuv[0]<=57 && cuv[0]>=48) if(cuv[0]>maxim[0] || ok==0) { ok=1; strcpy(maxim,cuv); } } cout< – Dr8gos Apr 03 '22 at 18:10
  • @Dr8gos dont put code in the comments, edit the question – pm100 Apr 03 '22 at 18:12
  • @it is initialized with 0 since is declared globally – Dr8gos Apr 03 '22 at 18:16
  • Re: `if(cuv[0]<=57 && cuv[0]>=48)` -- these magic numbers don't convey what the code is supposed to do. As a guess, could this be written as `if (std::isdigit(cuv[0])`? – Pete Becker Apr 03 '22 at 18:22
  • The type of `ok` should be `bool`. – Pete Becker Apr 03 '22 at 18:22
  • @AndreasWenzel -- the console transmits an EOF to the input stream when you type ^D on Unix systems or ^Z on Windows systems. – Pete Becker Apr 03 '22 at 18:25
  • OK so the actual question is "why doesnt this code exit the loop" – pm100 Apr 03 '22 at 18:28
  • @pm100 yeah.... – Dr8gos Apr 03 '22 at 18:29
  • becuase you have not done neither of the things I said in my answer. There is no 'break' in the loop so you have to type ctrl-d – pm100 Apr 03 '22 at 18:31
  • @PeteBecker: On platforms on which `char` is signed, the line `if (std::isdigit(cuv[0])` can invoke undefined behavior, unless the function argument is guaranteed to represent a non-negative value. Therefore, `if (std::isdigit(static_cast(cuv[0]))` would be safer. See [this link](https://stackoverflow.com/a/45007070/12149471) for further information. – Andreas Wenzel Apr 03 '22 at 18:41

1 Answers1

3

to exit that loop you have 2 choices

  • detect an end condition
  • close the input file

End condition (changed to use string)

 string word;

while(cin>>word)
{  
   if (word == "quit") break;
}

Close the input file

  • On linux or mac type ctrl-d at the console
  • on windows type ctrl-z

Your posted code fails because you did not initialize 'Ok'

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
pm100
  • 48,078
  • 23
  • 82
  • 145