1

Hi I am solving a question of book C++ Primer by Stanley. The question is :-

Write a program to read standard input a line at a time. Modify your program to read a word at a time.

I have used select variable through which user can switch to desired output i.e whether to print a line or a word. The Line output is coming right. But, the word output is not coming right. As, I want to print word before space. But it's printing whole sentence even after whitespaces.

Code below :-

 #include<iostream>
using namespace std;
int main(){
  char select;
  string line,word;
  cout<<"please enter w(word) or l(line)";
  cin>>select;
  if(select=='l'){
  /* program to read one line at a time */
  while(getline(cin,line)){
    cout<<line;
  }
  }
  else if(select=='w'){
  /*program to read one word at a time */
  while(cin>>word){
    cout<<word;
  }
  }
  else {
    cerr<<"you have entered wrong input!"<<endl;
    return -1;
  }
  
  return 0;
}

my output is coming is as follows when I'm selecting w :- enter image description here

I want it to print only shubharthak as I am only using cout<<word; It should not include whitespaces and only print characters before whitespaces. If this is not the case then why It print single word when I compile the following program :-

#include<iostream>
using namespace std;
int main(){
  string s;
  cin >> s;
  cout << s;

  return 0;
}

if I compile the above program, it will give output as follows,it will only print single word before whitespace :- enter image description here

  • It is printing the whole sentense because you told it to print all things read by using `while` statement. – MikeCAT Jun 11 '21 at 12:54
  • How can I fix it ? I want it to print a single word just like it prints in 2nd program – Shubharthak Jun 11 '21 at 12:56
  • Change the `while` to `if` so that the input and output will be executed only once. – MikeCAT Jun 11 '21 at 12:57
  • That said, *Modify your program to read a word at a time.* suggests that you do want the `while` loop. Other wise the instructions would read more like *Modify your program to read a word.* – user4581301 Jun 11 '21 at 13:03
  • @user4581301 Yes, That's what I'm thinking too, As it is said to take standard input which maybe means that it should take input till EOF. So using while loop for word should also be mandatory pardon me if I'm wrong. So, Do you know how can I use while loop and still can get the desired output i.e only a single word instead of the sentence when choosing **w** – Shubharthak Jun 11 '21 at 13:09
  • As long as the `while` is there the program will keep reading until the read fails and `cout< – user4581301 Jun 11 '21 at 13:16

1 Answers1

1

It's because of the while-loop. Remove it and the program work as expected.

#include<iostream>
using namespace std;
int main()
{
    char select;
    string line,word;
    cout<<"please enter w(word) or l(line)";
    cin>>select;
    if(select=='l')
    {
        while(getline(cin,line)) { cout<<line; }
    }
    else if(select=='w') { cin >> word; cout<<word; }
    else
    {
        cerr<<"you have entered wrong input!"<<endl;
        return -1;
    }

    return 0;
}

Result :

please enter w(word) or l(line)w
test1 test2 test3
test1

Related : cin inside a while loop

Also, see Why is "using namespace std;" considered bad practice?

silverfox
  • 1,568
  • 10
  • 27
  • But doesn't the program wants me to use while loop for print the word? As, it is said to take standard input which maybe means that it should take input till EOF. So using while loop for word should also be mandatory pardon me if I'm wrong – Shubharthak Jun 11 '21 at 13:07
  • @Shubharthak • *only print a single word* conflicts with *loop and print every word*. – Eljay Jun 11 '21 at 13:29
  • @Eljay Oh thank you that's what I was trying to know. So I can't by any chance get the single word while using the **while loop**. It will be appreciated if you can explain why that's the case? – Shubharthak Jun 11 '21 at 13:36
  • @Shubharthak Programming is logic. A loop is used when you want to **repeat** something. For instance, if your teacher tell you to read the first letter in the alphabet, do you read all of them? Sorry for the bad example, but " get the single word while using the while loop" is conflicting themselves. – silverfox Jun 11 '21 at 13:50
  • @Shubharthak • for the same reason "eat only a slice of pizza" and "eat all the pizza" are mutually exclusive. – Eljay Jun 11 '21 at 13:54
  • @Shubharthak So, decide on what you want the **computer to do** instead of copying code and prays that it works. From examples of mine and Elijay, you can see that there's no point looping here (of course, you can still do it if you want, it just very inefficient and pointless). – silverfox Jun 11 '21 at 13:57
  • @silverfox thank you so much buddy, However, I didn't copied it and was trying to do it by myself :) – Shubharthak Jun 11 '21 at 14:14