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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:50:54+00:00 2026-05-25T19:50:54+00:00

I tried to write a code that asked me to input numbers one by

  • 0

I tried to write a code that asked me to input numbers one by one and when a certain char was inserted ( in this case ‘x’ ) it would stop the loop. But when I insert that char it starts spamming me with “Insert Number” . I think that the fault is that I’m trying to insert a char in an int array, but I can’t think a way around it.

long int numbers[100]={0};
char h='y';
int index=0;
do
{
    cout << "Insert Number : ";
    cin >> numbers[index];
    h=(char)numbers[index];
    index++;
}
while(h!='x');
  • 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-25T19:50:54+00:00Added an answer on May 25, 2026 at 7:50 pm

    You should write a loop as:

    while(cin >> numbers[index]) 
       index++;
    

    It will read all the integers, untill you enter some invalid input, be it 'x' or any other character. Now if you want to skip all invalid inputs and continue reading integers (which might be after invalid inputs), and want to consider only 'x' to exit from the loop, then wrap the above loop with another loop as:

    char ch;
    do
    {
       while(cin >> numbers[index]) 
           index++;
       cin.clear(); //clear the error flags, so you can use cin to continue reading
       cin >> ch; //read the invalid character
    } while(ch != 'x');
    

    One piece of advice: prefer using std::vector<long int> over long int numbers[100]. What if user entered more than 100 integers, then your program will be corrupted.

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

Sidebar

Related Questions

This a code that would reverse the data of a document and save it
Right now I am trying to use selenium ide to write code that makes
I've written code that should ideally take in data from one document, encrypt it
I am a newbie. I have tried to write a template function that gets
This is probably a question that has been asked before, but I couldn't find
I have asked this question so many times and have tried so many different
For a class exercise on game trees , I have to write code that
I've seen this code that's been floating around, and also the fixed? version. Basically
While it seems that this has been asked many times before, but there's a
Today in my college a teacher asked me a question. He wrote this code

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.