My little self made application closes instantly

Viewed 94

I have written this little piece in C++ and if I open the program it should sum up the input then the "Press to finish" should be printed, and after it is printed the application should close after the user enters 'Enter'. The problem is it closes itself already after the numbers added are added.

#include <stdlib.h>
#include <stdio.h>

int multi(int a, int b){
    return a * b;
}
int main(){
    printf("Programm started \n");
    int number1;
    int number2;
    int sum;
    printf("Type two numbers: \n");
    scanf("%d %d", &number1, &number2);
    sum = multi(number1,number2);
    printf("The solution is: %d \n", sum);
    printf("Press <Return> to finish \n");
   char c = getchar();
    if(c=='\n'){
         return 0;
    }
    
    
}
5 Answers

I don't see you waiting for the user to hit enter:

char c = getchar();  // get the next available character
                     // If there is one then take it
                     // This only waits if there is no input available.

// This does nothing.
// If the character is '\n' it returns 0
// but if it is not the '\n character then you immediately exit
// In C++ if main does not have a final return the compiler plants
// a return 0 autoamtically so it also returns zero.
if(c=='\n'){
     return 0;
}
// implied return 0 here

So if I look at your other code:

scanf("%d %d", &number1, &number2);

This reads two numbers from the input. BUT it does not read the new line character. This means you still have a new line character left on the input stream which is why the getchar() at the end is returning immediately.

Change:

scanf("%d %d\n", &number1, &number2);
 //         ^  Specifically read a newline here.
 //
 // Be careful.
 // This is not a perfect solution. If there are any other characters
 // after your numbers this will fail (so you really should do some more
 // and better processing but this will resolve your immediate problem).
 //
 // I would add a loop here (just after you read your numbers)
 // to read and discard any junk on this line (or read and error 
 // if there is junk but be OK if the only input is white space
 // terminated by a new line).

When you press enter in scanf("%d %d", &number1, &number2); the new line character is going to be read by getchar and hence (c=='\n') is true.

what you could do is ignore that new line left by scanf like this :

char c = getchar();
if (c == '\n') c = getchar(); 
if (c == '\n')  {
     return 0;
}

If you print the value of c as an unsigned integer,

printf("%u\n",c);

then you'll see that it already has a value of 10 which is a newline. getchar returns the next character from the standard input which means you still have a newline character on the input stream from your previous scanf calls.

scanf("%d %d\n", &number1, &number2);

will specifically read in that hanging new line.

scanf only reads your two inputs from the input buffer and leaves the '\n' when you enter the first two numbers and hit enter, which later on is ready by getchar().

To read a line including spaces, do not use scanf("%s",....) Consider fgets().

fgets(string, sizeof string, stdin);
// remove potential trailing \r\n as needed
string[strcspn(string, "\n")] = 0;

Alternatively you could use %*c. Check the live code here.

int main(){
    printf("Program started \n");
    int number1;
    int number2;
    int sum;
    printf("Type two numbers: \n");
    scanf("%d %d%*c", &number1, &number2);  // Add %*c here
    sum = multi(number1,number2);
    printf("The solution is: %d \n", sum);
    printf("Press <Return> to finish \n");
   char c = getchar();
   printf("c: %c", c);
    if(c=='\n'){
        printf("hi");
         return 0;
    }
    
}

%*c will accept but ignore any following characters.

If you were using i/o streams then this works.

#include<iostream>

int main()
{
     int dummy = 0;
     std::cout << "Enter A number to exit" << std::endl;
     std::cin >> dummy;

    return 0;
}

Keeps the window open until you enter something, anything will do.

Related