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 7440047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:47:34+00:00 2026-05-29T10:47:34+00:00

I am writing a program in C++ which is supposed to take an input

  • 0

I am writing a program in C++ which is supposed to take an input string from user, print it and ask the user to type in another string again and print it again until the user presses the return key without typing any string. In that case the program should exit.

I came up with the following, but it doesn’t work as desired. Any ideas?

int main(){

    string surname;
    int c;
    while (true) {
        surname = "";
        cout << "Enter surname (RETURN to quit): ";
        c = cin.get();
        if (c == '\n') {
            break;
        }
        cin >> surname;
        cout << surname << endl;
    }
    return 0;   
}
  • 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-05-29T10:47:35+00:00Added an answer on May 29, 2026 at 10:47 am

    I will give you some tips so that I don’t solve the problem for you, but help you along.

    You don’t need to read one character at a time, you can read each line into a string variable. Also, you need to use std::getline(std::cin, stringvariable) instead of cin >> stringvariable because the latter will skip all newlines until it gets to a non-newline character, so pressing enter by itself will not do anything.

    After using std::getline, you can tell if the user entered anything by comparing the length of the string with 0 or calling empty on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing installation script for my program, which is supposed to run on
I am writing a C# program which captures signals from a external device, and
I wrote a program which includes writing and reading from database. When I run
I'm writing a program which is supposed to read two strings that can contain
I am writing a program which will type a line of text in 5
I am writing a program which makes a RPC to print a message that
I am writing a program which has two panes (via CSplitter ), however I
I'm writing a program which has both an ASP.NET configuration system and a Silverlight
I am writing a program which if I compile on a Suse 10 32-bit
I'm writing a program which will use scan conversion on triangles to fill in

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.