Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Requirement:
1. ask user to input value for x,y,z for mathematic equation
2. check the value is not alphabet- ask if want to re input
3. calculate the answer if all the input is numeric
3. loop when user wish to continue for the next input

Problem:
Run it and found out can input infinite without execute other code. when key in value for x,y,z

1. can key in infinite number when the input is integer
Enter real number X:
Enter real number Y:
Enter real number Z:
1
1
1
1

2. when key in alphabet code keep looping until printf() continue
Enter real number X:
Enter real number Y:
Enter real number Z:
Entered value is not numeric
Continue (Y/N)? y
Enter real number X:
Enter real number Y:
Enter real number Z:
Entered value is not numeric
Continue (Y/N)?


3. after that tried to input integer then alphabet then integer somehow it can run to the end

Enter real number X:
Enter real number Y:
Enter real number Z:
Entered value is not numeric
Continue (Y/N)? 1
1
1
1
1
1
f
Entered value is not numeric
Continue (Y/N)? 1
1
1
X=1.0000 ; Y=1.0000 ; Z=1.0000
f(x,y): -8.0000; g(x,z): 0.0000; h(x,y,z): -inf
Do you want to continue Y/N:

What I have tried:

C++
do
    {
        printf("Enter real number X: \n");
        //scanf("%f",&X);
         
        printf("Enter real number Y: \n");
        //scanf("%f",&Y);
         
        printf("Enter real number Z: \n");
        //scanf("%f",&Z);
         
         while(1)
        {
            if (scanf("%f%f%f", &X,&Y,&Z) != 3)
            {
            printf("Entered value is not numeric\n");
            printf("Continue (Y/N)? ");
            scanf(" %c", &choice);
            }
            if ((choice != 'Y') && (choice != 'y')) break;
        }
         
            if(scanf("%f%f%f", &X, &Y, &Z) == 3)
            {
             
   //continue to calculate the equation and print result
            }
Posted
Updated 1-Sep-16 5:34am
v2
Comments
Leo Chapiro 1-Sep-16 10:58am    
And what is exctly the problem with currently solution?
JiaWei Lee 1-Sep-16 11:06am    
Infinite enter of input if all is numeric by right only 3
Then when enter an alphabet the code looping again and again showing the printf ( ask to continue)

I have describe the situation in the question

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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