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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:01:08+00:00 2026-05-27T10:01:08+00:00

I wrote this code in C++, and I used getchar() to puase the console,

  • 0

I wrote this code in C++, and I used getchar() to puase the console, but I did not see any effect of using that function, here is the code:

#include<iostream>
#include<stdio.h>//to pause console screen

using namespace std;
//function prototypes
int  getSmallest(int*);
int getOccurrence(int, int*);

int main(){

    int a[7], counter=0, smallest;
    cout<<"Please enter 7 integers"<<endl;
    while(counter!=7){
        cin>>a[counter];
        counter++;
    }
    smallest=getSmallest(a);
    cout<<"The smallest number is "<<smallest<<"; it apears "<<getOccurrence(smallest,a)<<" times"<<endl;
    getchar();//to pause console screen
    return 0;
}

int  getSmallest(int*x){
int count=0, smallest=*x;
//loop till last item in array
while(count!=7){

    if(*x<smallest)
        smallest=*x;
    count++;
    x++;
}
return smallest;
}


int getOccurrence(int smallest, int* address){

int count=0,occ=0;
//loop till last item in array
while(count!=7){

    if(*address==smallest)
    occ++;
    count++;
    address++;
}
return occ;

}
  • 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-27T10:01:09+00:00Added an answer on May 27, 2026 at 10:01 am

    As has been pointed out, the issue is that your input buffer had a string for the number AND a newline. C++ I/O skips leading whitespace when it reads something like a number out, but it doesn’t take the trailing whitespace out of the buffer. It leaves that for the next read to deal with. So getchar() is getting that newline that’s still pending.

    Ignore advice from people who are trying to tell you to flush(), ignore(), or clear out “whatever was in the buffer before the getchar() call”. These functions have no notions of “non-blocking” input.

    Said another way: the usual C++ input stream functions don’t have a concept of “there’s nothing there right now”…but then you call later and it says “oh, but now there’s something!!!” There’s an “input sequence” that you can only detect as stopping when you hit EOF.

    The exception to this would be readsome()…which rather than operating on an “input sequence” operates on the “input buffer”. Finding such a thing, we might try this:

    #include<iostream>
    #include<cstdio>//to pause console screen
    
    using namespace std;
    
    int main(int argc, char* argv[]) {
        cout << "Enter a number\n";
        int num;
        cin >> num;
    
        char ch;
        while (cin.readsome(&ch, 1) != 0)
             ;
    
        cout << "Press any key to continue...\n";
        getchar();
        return 0;
    }
    

    But at least on my machine, it doesn’t lead to the desired effect. Which means that even though there’s a newline sitting in the terminal app or OS pipeline somewhere, it hasn’t yet gotten as far as the internal stream buffer object for cin. Upshot is: there is a non-blocking input function based on the buffer, but in this kind of scenario it apparently isn’t going to help.

    The real answer is what Alf said in the comment. Most decent dev environments or setups will have some way to configure it to not let the console close automatically. If not, hack around it with your launch method. Heck, you can even put a breakpoint on the return 0 in main!


    Notes:

    You should be aware that “correct” C++ inclusions of compatibility libraries for C are done as #include <cfoo> instead of #include "foo.h". It may not make all that big a difference in practice…but at minimum it distracts from your question when people comment about it (like I’m doing right now):

    Is it bad practice to use a C header instead of its C++ equivalent in C++ (e.g. stdio.h instead of cstdio)?

    Also, you could have demonstrated this with a much smaller sample! You could have shown the effect simply with:

    #include<iostream>
    #include<cstdio>//to pause console screen
    
    using namespace std;
    
    int main(int argc, char* argv[]) {
        cout << "Enter a number\n";
    
        int num;
        cin >> num;
    
        cout << "Press any key to continue...\n";
        getchar();
        return 0;
    }
    

    So try and pare down your examples to really isolate the problem in the future! (Feel free to edit your question to be this succinct in order to make this thread more useful for people searching.)

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

Sidebar

Related Questions

I wrote this code. The constructor works normally, but in the destructor I get
I want to convert function object to function. I wrote this code, but it
I wrote this snippet of code and I assume len is tail-recursive, but a
I'm using code that someone else wrote to load a file format that I
I'm not used to having to take i18n stuff into account. I wrote this
this is the code i wrote to send the url request using a thread:
I have this code that someone else wrote and am wondering, what this jQuery
I have a compiled external library that I'm using in my (Objective-C++) code. This
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this code in C# to encrypt a string with a key: private

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.