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

  • Home
  • SEARCH
  • 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 8235955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:59:26+00:00 2026-06-07T18:59:26+00:00

I have the following code: main.cpp #include <iostream> #include <string> using namespace std; string

  • 0

I have the following code:

main.cpp

#include <iostream>
#include <string>

using namespace std;

string name;
string age;

int main() {
    cout <<"Name: ";
    cin >> name;
    cout << endl;
    cout <<"Age: ";
    cin >> age;
    cout << endl;
    cout << "Your name is " << name << ", and you are " << age << " years old."  << endl;
    cout << "Press enter to close this application" << endl;
    getchar();
    return 0;
}

I noticed that if I put a space in my input for name that it won’t give me a chance to input name, and it will view the entry after the space as age. I apologize if this is a newbie mistake, which it probably is. I previously programmed Java and decided I wanted to switch to C++ because it better suits my needs. I also probably format my code weird to your standards, please correct it if you wish to.

Screenshot of console

I’ve also noticed another error, something I never really had any problems with in Java. I can’t figure out how to prevent it from instantly closing down when it finishes processing. I’ve heard you can use "system.("pause"); but I’ve also been told to not use it. I’m really confused on what to use. I’ve heard to use getchar();, but it doesn’t seem to do anything.

Any help would be greatly appreciated, as I’m a complete beginner when it comes to C++.

  • 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-07T18:59:29+00:00Added an answer on June 7, 2026 at 6:59 pm

    Here’s what’s happening with the input buffer when you run your program:

    std::cin >> name;
    

    You’re waiting for input. When you enter “Ryan Cleary”, and press enter, the input buffer contains:

    Ryan Cleary\n
    

    Now your cin reads input as normal, stopping at whitespace, leaving your buffer like this:

     Cleary\n
    

    Note the beginning space, as it stops after reading Ryan. Your first variable now contains Ryan. If, however, you want the full name, use std::getline. It will read until a newline, not just whitespace. Anyway, continuing on:

    std::cin >> age;
    

    Now you’re getting another input. There’s already something there, though. It skips the whitespace until it can start reading, leaving the buffer with just:

    \n
    

    Your second variable gets the text Cleary. Note the newline still in the buffer, which brings me to the second part. Replacing system ("pause"); in a way that always works is tricky. Your best bet is usually to live with a less-than-perfect solution, or as I like to do, one that isn’t guaranteed to do exactly what it says:

    std::cin.get(); //this consumes the left over newline and exits without waiting
    

    Okay, so cin.get() didn’t work. How about this:

    std::cin.get(); //consume left over newline
    std::cin.get(); //wait
    

    That works perfectly, but what if you copy-paste it somewhere where the newline isn’t left over? You’ll have to hit enter twice!

    The solution is to clear the newline (and anything else) out, and then wait. This is the purpose of cin.sync(). However, as seen in the notes section, it is not guaranteed to clear the buffer out like it says, so if your compiler chooses not to, it can’t be used. For me, however, it does exactly that, leaving a solution of:

    std::cin.sync(); //clear buffer
    std::cin.get(); //wait
    

    The main bad thing about system("pause"); is that you have no idea what program it will run on someone else’s computer. They could’ve changed pause.exe or put one that’s found first, and you have no way of knowing. This could potentially ruin their computer due to it being possibly any program.

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

Sidebar

Related Questions

i have following code in c++ #include <iostream> using namespace std; void qsort5(int a[],int
I have the following C++ code. #include <iostream> using namespace std; int testfunction(int input)
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
i have following code for heapsort #include <iostream> using namespace std; void exch(int a[],int
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
I have the following C++ code: #include <iostream> #include <string> using namespace std; class
I have the following source code in main.cpp: #include <iostream> #include <iomanip> int main()
I have the following code which works as expected: #include <iostream> using namespace std;
I have the following code :- #include <iostream> #include <vector> using namespace std; template

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.