Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:01:10+00:00 2026-06-02T12:01:10+00:00

Here is my code, If you enter Ctrl+Z it should finish, but it will

  • 0

Here is my code, If you enter “Ctrl+Z” it should finish, but it will go into loop mode and it repeats writing the last word.

#include <iostream>
using namespace std;
int main()
{  char word[80];
   do
   {  cin >> word;
      if (*word) cout << "\t\"" << word << "\"\n";
   }  while (*word);
}

By Pressing “Ctrl+Z” this simple program should end, but why it is not going in this way? what is problem with it?

Look at this code:

{  char line[80];
   do
   {  cin.getline(line,80);
      if (*line) cout << "\t[" << line << "]\n";
   } while (*line);
}

it is similar to the first code, but this time it is working fine and it will exit loop by pressing “Ctrl+Z”

So what is the exact problem with first code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:01:11+00:00Added an answer on June 2, 2026 at 12:01 pm

    It doesn’t exit the loop because *word is not null-character; word still points to the buffer read in the previous iteration, and it does gets erased when you press Ctrl+Z.

    You should write the loop as:

    while(cin >> word) { /* your code */ }
    

    then it should exit the loop once you press Ctrl+Z

    Also, it is better to declare word as std::string instead of char[80]:

    std::string word; //#include <string>
    
    while (cin >> word)
    {  
      cout << "\t\"" << word << "\"\n";
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove annota enter code here tion some a MKMapView but am
enter code here I've created a simple code for mount virtual disk into system
enter code here I have a table on SQL server 2005 with bigint primary
enter code here Hi All, I have a simple windows service application that connects
enter code here I have a ListView that have a different Layout XML for
enter code here My problem is this: in this database the junction table contains
I check like this: enter code here var point = new g.LatLng(parseFloat(lat),parseFloat(lng)); var bounds
I have <.ImageView> enter code here s(icons) in my android layout xml file, which
i have two dropdown list. first drop down:1 enter code here <form:select path=custName id=custName>
enter code here I'm reading a stream from a device in Linux which contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.