1

My code isn't working after the second scanf statement. I'm still new to C++ so im not sure what I am doing wrong. I've looked on this website as well as others and so far havent found anything. I've also tried the fflush statement but that didnt fix it either, any other suggestions would be great thanks.

include <stdio.h>

int main(void)
{
    //Intro message
    printf("This program will take the order of a customer and then subtract their order from the inventory list. After calculations a new inventory list will appear\n");

// declare variables
int dough, sauce, cheese, pepperoni, sausage, greenPeppers, blackOlives, mushrooms;
int num1; 
char pizza;

// create inventory
dough = 50;
sauce = 50;
cheese = 50;
pepperoni = 50;
sausage = 50;
greenPeppers = 50;
blackOlives = 50;
mushrooms = 50;
//display the inventory
printf("\n The currrent inventory is: \n Dough:%d", dough);
printf("\n Sause:%d", sauce);
printf("\n Cheese:%d", cheese);
printf("\n Pepperoni:%d", pepperoni);
printf("\n Sausage:%d", sausage);
printf("\n Green Peppers:%d", greenPeppers);
printf("\n Black Olives%d", blackOlives);
printf("\n Mushrooms:%d", mushrooms);

//get customer input

printf("\nWhat type of pizza would you like? Please type a lowercase letter and only chose one. Type C for Cheese, P for Pepperoni, S for Sausage, and V for Veggie.\n");
scanf_s(" %c", &pizza);

 printf("\nHow many pizzas would you like to order? Maximum of 10.\n");
 scanf_s("  %d", &num1);

//This is where it stops
 printf("It cuts out here, why can I not see anything after this line?");


// calculate what inventory will be taken out
cheese = cheese - num1;
dough = dough - num1;
sauce = sauce - num1;
switch (pizza)
{
case 'c': 
    cheese = 50 - num1;
    break;
case 'p':
    pepperoni = pepperoni - num1;
    break;
case 's':
    sausage = sausage - num1;
    break;
case 'v':
    blackOlives = blackOlives - num1;
    greenPeppers = greenPeppers - num1;
    mushrooms = mushrooms - num1;
    break; 

}

// display final inventory

printf("The new inventory is: \n Dough:%d", dough);
printf("\n Sause:%d", sauce);
printf("\n Cheese:%d", cheese);
printf("\n Pepperoni:%d", pepperoni);
printf("\n Sausage:%d", sausage);
printf("\n Green Peppers:%d", greenPeppers);
printf("\n Black Olives%d", blackOlives);
printf("\n Mushrooms:%d", mushrooms);


return 0;

}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 11
    ***I'm still new to C++*** Does not seem like you are using `c++` at all. This looks like `c` code. A `c++` solution would be totally different. – drescherjm Feb 15 '18 at 17:12
  • 1
    suggested reading: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Feb 15 '18 at 17:13
  • 2
    The expression "isn't working" is too vague. What are the inputs? What is the expected output? What is the actual output? How do they differ? Are there run-time errors? Please edit your post with the text of the answers; no links, no screen snapshots. – Thomas Matthews Feb 15 '18 at 17:16
  • 1
    Debugger. Use a debugger. A debugger allows you to single step through your program, watching values in variables. Often, using a debugger is faster than posting correctly to StackOverflow and *waiting* for somebody to inspect your code or debug it for you. – Thomas Matthews Feb 15 '18 at 17:17
  • 2
    I change the tag back to c++, because OP explicitly mentions that it is supposed to be c++. Even if it looks like c, if OP uses a c++ compiler then it is c++ (though not the nicest one) – 463035818_is_not_an_ai Feb 15 '18 at 17:17
  • @Aditi While you are correct that the OP is using C-like idioms, if they are compiling as C++ then it is a C++ question and should not be retagged by a third party. – Lightness Races in Orbit Feb 15 '18 at 17:24
  • @LightnessRacesinOrbit okay..point taken. :) – Aditi Rawat Feb 15 '18 at 17:28
  • in switch . case 'v' needs scope determiner { } – hamed Feb 15 '18 at 17:50
  • I run your code and if I Enter only one character after scanf. code work properly. but if I enter 2 char I saw exception. do you your problem is same with me? – hamed Feb 15 '18 at 17:54

1 Answers1

1

The problem is that you are using scanf (or its cousin scanf_s). The scanf problem is not suitable for interactive input, and leads to confusing results like what you're seeing.

Instead, use the fgets function to read data into a character array. If you need to convert that character array into a number, use atoi.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Why is scanf not suitable for interactive input? I'm not sure what else it would be used for. – john Feb 15 '18 at 17:22
  • are you saying to use fget for the char input or for the int input. or for both? –  Feb 15 '18 at 17:26
  • @john: `scanf` is sometimes suitable for reading formatted input from a file. However, when used interactively it *reads* everything up until you press Enter, but doesn't *consume* everything - and buffers what it hasn't consumed until the next call to `scanf`. – Greg Hewgill Feb 15 '18 at 17:33
  • @N.Rice: I am suggesting to use `fgets` in *all* cases instead of `scanf` for interactive input. You can use it for character input too, just look at the first character of the line the user typed. – Greg Hewgill Feb 15 '18 at 17:33
  • wait if i copy the lines :printf("\nHow many pizzas would you like to order? Maximum of 10.\n"); scanf_s(" %d", &num1); and then add them on the bottom right before return 0; my code works all the way but it ask for the pizzas again. Why does it do this. Also, if i run it like i posted above in a normal command window and not in visual studios it works with no errors the way it is supposed to. –  Feb 15 '18 at 17:35
  • 1
    @Greg Hewgill also the fgets did work, without having to add that last two lines of code at the bottoms so thanks for showing me that method. –  Feb 15 '18 at 17:40