#why doesn't it scanf a second time when I call the main() function?

140 messages · Page 1 of 1 (latest)

noble geyser
#

https://onlinegdb.com/nHASuqC74
since it doesnt scanf a second time NumCheck stays at 0 and it breaks into an infinite loop

carmine gustBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

elfin gorge
mint rock
#

you have a userinput function, but you don't use it when taking input for numcheck

noble geyser
#

so I cant call main

mint rock
noble geyser
mint rock
#

also, you have an infinite loop

noble geyser
#

so instead of calling main I should make another loop

mint rock
#

"while(1)" will run forever

noble geyser
mint rock
#

what do you want to do

noble geyser
#

calc

mint rock
#

calculate what

noble geyser
#

Im trying to make a calc

#

the first number is not in UserInput and thats intentional

#

second number onwards will be in there

#

its so i dont end up repeating asking for 1st num input again and again

mint rock
#

i see

#

as it is, the program is kinda confusing

noble geyser
#

so the main thing Im trying to do rn is figure out how to filter out non numerical inputs

#

but when I loop it the scanf function doesnt seem to work?

#

for some odd reason

#

see if the scanf function works it'd ask for an input

mint rock
noble geyser
#

but no, it just infinite loops

mint rock
#

this does not do what you think it does

noble geyser
mint rock
#

scanf returns the number of input items successfully matched

noble geyser
#

right.

mint rock
#

so since you're only matching one input

#

NumCheck will always be equal to 1

#

if there was an input

noble geyser
#

and NumCheck would always be equal to 0 if there wasnt a valid input?

mint rock
#

in this case

#

yes

noble geyser
#

is there a way for me to reset NumCheck

mint rock
#

you can have it inside the loop for one

#

but also, you can just skip having the numchecker function entirely

#

and check the return value of scanf

noble geyser
mint rock
#

you want to know if the user entered a valid number

noble geyser
#

yes

mint rock
#

instead of doing numChecker(numcheck)

#

you could just do if (numcheck == 1)

noble geyser
#

okay yeah that makes sense

#

but it would still break, no?

#

yep

#

still breaks

#

fuckk

#

seems like I also broke the calc

#

while trying to add the num checking fucntionality

#

https://onlinegdb.com/meUBfSUst
[broken] with the broken num checking

#

[Fixed] without broken num checking
https://onlinegdb.com/POw8PSClM

#

Im going to hit the sack for now

mint rock
#

i assume you mean you fixed it completely?

noble geyser
#

I still want to try and add that If I could

mint rock
#

ah i see

noble geyser
#

so once for first input

#

and one in UserInput

mint rock
#

hmm

noble geyser
#

im actually going to go get some rest now

#

ok so I figured something out

#
#

I could break out of the while loop

#

so it would still break the calc, but it would now say that it did

#

ig thats the best i can do for now

#

C's hard

mint rock
#
#include <stdio.h>

int UserInput(char* Operation, double* Num){
    printf("\nInput Operation: ");
        if ( (scanf(" %c", Operation)) != 1 ) {
          return 1;
        }
    printf("\nInput another Number: ");
        if ((scanf("%lf", Num)) != 1) {
          return 1;
        }
    return 0;
}


double Arithmetic(double X, double Y, char op){
    switch (op){
        case '+':
            return (X+Y);
        
        case '-':
            return (X-Y);
            
        case '/':
            return (X/Y);
        
        case '*':
            return (X*Y);
            
    }
}
// // int NumChecker(int X){
// //     if (X==1){
// //         return (X);
// //     }else {
// //         printf("\nNot a Valid Number\n");
// //         // int X = scanf("%lf", &Result[0]);
// //     }
    
// }

int main(){
    
    int i=0;
    int state;
    double Result[100], Num[100];
    char Operation;
    
    while (1){
    printf("Input your Number: ");  
    scanf("%lf", &Result[0]);
    while (1){
    
        state = UserInput(&Operation, &Num[i]);
        if (state == 0 && Operation == '-'|| Operation =='+' || Operation =='/' || Operation =='*') {
         Result[i+1] = Arithmetic (Result[i], Num[i], Operation);
         printf ("\nResult: %.2lf\n", Result[i+1]);
         i++;
        }else {
            printf ("Invalid Operator or Number\n");
        }        
        
    }
    }      
    return 0;
}
#

i just changed UserInput's return type to an int

#

this way it would return 1 incase of invalid input, and 0 otherwise

#

you could store this in a state variable to check if the input was valid or not

noble geyser
#

alright yeah so your way works

#

but strangely it would still print out "Input Operation:" and "Input another Number:" before printing out the invalid message

#

I tried to look for a way around that but came up short

noble geyser
#
mint rock
#

oh yeah

noble geyser
#

other than that the calc's pretty much done

mint rock
#

probably

#

checking the scanf return for the operator is useless

#

because its a char, so it'll accept any input

noble geyser
noble geyser
mint rock
#

i fixed it

#

too

#

ah

noble geyser
#

How'd you fix it?

mint rock
#

that works too

mint rock
# noble geyser How'd you fix it?
#include <stdio.h>

int UserInput(char* Operation, double* Num){
    printf("\nInput Operation: ");
        if ( (scanf(" %[+*-/]c", Operation)) != 1 ) {
          scanf("%*[^\n]");
          return 1;
        }
    printf("\nInput another Number: ");
        if ( (scanf("%lf", Num)) != 1 ) {
          scanf("%*[^\n]");
          return 1;
        }
    return 0;
}


double Arithmetic(double X, double Y, char op){
    switch (op){
        case '+':
            return (X+Y);
        
        case '-':
            return (X-Y);
            
        case '/':
            return (X/Y);
        
        case '*':
            return (X*Y);
            
    }
}
// // int NumChecker(int X){
// //     if (X==1){
// //         return (X);
// //     }else {
// //         printf("\nNot a Valid Number\n");
// //         // int X = scanf("%lf", &Result[0]);
// //     }
    
// }

int main(){
    
    int i=0;
    int state;
    double Result[100], Num[100];
    char Operation;
    
    while (1){
    printf("Input your Number: ");  
    scanf("%lf", &Result[0]);
    while (1){
    
        state = UserInput(&Operation, &Num[i]);

        if (state == 0 && Operation == '-'|| Operation =='+' || Operation =='/' || Operation =='*') {
         Result[i+1] = Arithmetic (Result[i], Num[i], Operation);
         printf ("\nResult: %.2lf\n", Result[i+1]);
         i++;
        } else {
            printf ("Invalid Operator or Number\n");
        }        
        
    }
    }      
    return 0;
}
#

you can specify which values scanf should accept using []

#

so i put all the operators in it

noble geyser
#

Omg my eyes

mint rock
#

the problem that i then ran into was because we use scanf

noble geyser
mint rock
#

if you use scanf, and it finds a non-matching character

mint rock
#

should be black on black

noble geyser
#

It's probably os side

mint rock
#

i guess

noble geyser
#

Whatever you did is out of my skill level rn

mint rock
#

i dont have an account on gdb so cant paste my code there lol

mint rock
noble geyser
#

So it's not going to be what I submit in the end, though I appreciate the help

mint rock
#

only confusing part is scanf("%*[^\n]");

noble geyser
mint rock
#

you found a solution too

#

so it worked out in the end

noble geyser
#

I can't be learning struct and flush or whatever

mint rock
#

oh the flush isnt needd

#

i forgot to remove it

#

but yeah i understand

noble geyser
# mint rock so it worked out in the end

Yeah. Your original way makes sense to me i just don't get why it prints and asks for the operator and second number even if the first number is alr wrong. Doesn't really mess anything up though

#

It's more of a "would be nice if it didn't"

mint rock
#

but our operator is a char

#

and char can accept all alphabets and numbers

#

so it'll always return true

noble geyser
#

Wait so why did it still do stuff even after I removed it from the char scanf?

mint rock
#

so it only checked if the number was valid or not

noble geyser
mint rock
#

same reason

#

it doesnt check if operator is a valid operator

#

as long as its a char

noble geyser
#

I'll check your solution tommorow, I alr have my laptop off

#

And I don't think my 1 am brain is proeprly comprehending what you're trying to tell me

mint rock
#

okay!

#

me neither, pretty late here

noble geyser
#

Thanks for the help.

mint rock
#

gnight!

carmine gustBOT
#

@noble geyser Has your question been resolved? If so, run !solved :)

carmine gustBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.