0

Not really interested in finishing project I am more interested in knowing why I can print the answer variable after the while loop. I know it works if I have it inside the while loop. I know I get an error code -1073741819 which meant that the loop terminate abortly or there is something wrong after the while loop ended

int main(){

int number;
int number2;
char *sign;
char *quit = {"h"};
int answer;
while (quit[0] != 'q'){
    printf("Enter a num");
    scanf(" %d", &number);
    printf("Enter a second num");
    scanf(" %d", &number2);
    printf("Enter a sign");
    scanf(" %c", &sign);
    printf("enter a q to quit");
    scanf(" %c", &quit[0]);
    if (sign == "+"){
        answer = number + number2;
    }
}
cout << answer; //Why wont this print
cout << "Program ended";

return 0;

}

Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30
Sample
  • 15
  • 4
  • 3
    Your program doesn't print anything after the loop because it never gets to the code after the loop - it crashes sooner. This program exhibits undefined behavior. In `scanf(" %c", &sign);`, `%c` specifier expects an argument of type `char*`, but you are passing `char**`. `scanf(" %c", &quit[0]);` attempts to modify a const object (the first character of a string literal). – Igor Tandetnik May 02 '21 at 15:20
  • 1
    If you translate that number to hex it is `C0000005`, which signifies an access violation. https://stackoverflow.com/a/17169463/920069 – Retired Ninja May 02 '21 at 15:27

1 Answers1

4

As @Igor Tandetnik specified above, "scanf(" %c", &sign); will expects an argument of type *char and scanf(" %c", &quit[0]); will attempts to modify a const object (the first character of a string literal)"

I modified the code as such:

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    int number;
    int number2;
    char sign;
    char quit = '-';
    int answer;
    while (quit != 'q'){
        printf("Enter a num: ");
        scanf(" %d", &number);
        printf("Enter a second num: ");
        scanf(" %d", &number2);
        printf("Enter a sign: ");
        scanf(" %c", &sign);
        printf("Enter a q to quit: ");
        scanf(" %c", &quit);
        if (sign == '+'){
            answer = number + number2;
        }
    }
    cout << answer <<"\n"; 
    cout << "Program ended";

    return 0;
}

Results:

Enter a num: 2
Enter a second num: 3
Enter a sign: +
Enter a q to quit: 1
Enter a num: 2
Enter a second num: 3
Enter a sign: +
Enter a q to quit: q
5
Program ended

And as @Retired Ninja mentioned, error code -1073741819 translate to C0000005 in hex, which according to this answer:

Exception code C0000005 is the code for an access violation. That means that your program is accessing (either reading or writing) a memory address to which it does not have rights. Most commonly this is caused by:

  • Accessing a stale pointer. That is accessing memory that has already been deallocated. Note that such stale pointer accesses do not always result in access violations. Only if the memory manager has returned the memory to the system do you get an access violation.
  • Reading off the end of an array. This is when you have an array of length N and you access elements with index >=N.