-2

scanf statement after if block is not working can somebody help me please

#include<stdio.h>
    main()
    {

        int input,itemno,input2;
        int name1,price1,name2,price2,name3,price3;
        printf("enter input:\n");
        scanf("%d", &input);
        if(input==1)
        {
            printf("enter number of items:\n");
            scanf("%d",&itemno);
            if(itemno<=3)
            {
                printf("enter name and age:\n");
                scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);
            }
         else
            printf("you can only enter 3 students");
     }
        printf("press 1 to enter again: \n");
        scanf("%d",&input2);
        if(input2==1)
        {
            printf("hey");
        }
    }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
anam
  • 1
  • 1
  • unchecked scanf return value... I smell danger.... – Sourav Ghosh Nov 02 '16 at 06:39
  • Please don;t break the formatting. Read the posting guides. – Sourav Ghosh Nov 02 '16 at 06:40
  • Are you supposed to enter *numbers* for the "name"? Why do you need to enter name and prices three times? Why aren't you telling the user that he or she needs to enter three names and prices? – Some programmer dude Nov 02 '16 at 06:41
  • Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Just saying "is not working" is not enough. ***How*** is it "not working"? Have you checked what [`scanf`](http://en.cppreference.com/w/c/io/fscanf) returns? Have you used a debugger to step through the code? That information should be in the question body. – Some programmer dude Nov 02 '16 at 06:43
  • I don't understand what this code is trying to do. Please clarify what you want to achieve with a sample output. – RoadRunner Nov 02 '16 at 08:14

2 Answers2

0
scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);

Don't use escape characters in scanf function call.

Vijay S B
  • 1,251
  • 1
  • 13
  • 24
0

just delete the last \n in your scanf the result is : result

guoqiang Wei
  • 83
  • 2
  • 6