Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I've tried to print different values for different inputs . But here to every input it shows Error !!! can you guys explain what mistake i did here?
I'll post my code bellow

What I have tried:

#include <stdio.h>

int main()
{
    int i =0,h,m,f;
    int totH,totM,totF;
    char type;
    
    printf("Type\t Course Name\t\tRegister fees\n");
    printf("H   \tDip in hospitality\t 1500.000\n");
    printf("M   \tDip in Marketing\t 2000.000\n");
    printf("F   \tDip in Finance\t\t 2500.000\n\n");
    
    for (i=0;i<=100;i++)
    {
        printf("Enter your course type (H/M/F):");
        scanf("%*c%c",&type);
       
        if (type=='h'||type=='H')
        {
            printf("You have selected Dip in hospitalty");
            printf("course fee is 1500.00");
            h=h+1;
            totH=totH+1500;
        }
        else if (type=='m'||type=='M')
        {
            printf("You have selected Dip in Marketing");
            printf("course fee is 2000.00");
            m=m+1;
            totM=totM+2000;
        }
        else if (type=='f'||type=='F')
        {
            printf("You have selected Dip in finance");
            printf("course fee is 2500.00");
            f=f+1;
            totF=totF+2500;
        }
        else
        {
            printf("Error\n");
        }
        
    }
    
    printf("Registered number of students for hospitality : %d ",h);
    printf("Registered number of students for marketing : %d ",m);
    printf("Registered number of students for finance : %d ",f);
    
    
    printf("Total amount for hospitality :%d",totH);
    printf("Total amount for marketing : %d",totM);
    printf("Total amount for finance : %d",totF);
    
    return 0;
}
Posted
Updated 15-Jun-21 15:08pm
Comments
Patrice T 14-Jun-21 22:04pm    
"But here to every input it shows Error !!!"
would be nice to show us error messages too.
Member 15226818 14-Jun-21 22:23pm    
Yeah I mean , in my nested if else statement , I typed an error message to be printed when user inputs a wrong type. But even if i entered a correct type , then also it prints the error message !!!
Patrice T 14-Jun-21 22:31pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
KarstenK 15-Jun-21 2:11am    
Did you use the debugger?

Quote:
But here to every input it shows Error !!!

You should make sure
C++
scanf("%*c%c",&type);

is correct.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 15-Jun-21 1:51am    
5.
Patrice T 15-Jun-21 1:56am    
Thank you
It is because of your scanf format string. You are using "%*c%c", which ignores any characters entered. It should just be "%c".
 
Share this answer
 
Comments
Member 15226818 15-Jun-21 21:23pm    
Yea bro , if i enter "%c" ,it automatically takes an input and shows the print function that written in else part.
Richard MacCutchan 16-Jun-21 4:12am    
If you use the simple "%c" then scanf will capture the first character that you enter. But you also need to consume the \n character at the bottom of the loop. So add scanf("%*c"); at the bottom of the loop.
Hi:

Here is the problem
scanf("%*c%c",&type);


You are ignoring the first char and taking the second one, so your selection is lost and, I presume, you are getting the <enter>

Angel
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900