I am writing a small algebra io program for an intro to c class and i am stuck. The idea is to get the user to input (a * b) + (c / d) + (e % f), in exactly the same format as you see it with the exception of using int values for the letters. I am using DevC++ form bloodshed (currently no longer on the web), and their program is getting the math wrong for the first operation. say i put in
(5 * 5) it tells me that it is 5 * 36
Here is my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main() 
 printf("Enter equation exactly as you see, inserting an INT for a, b, and c\n(a * b): \n");
     scanf("%c ,%d ,%c ,%d ,%c",&a,&myVarA,&c,&myVarB,&e);
     mySum1=myVarA*myVarB;
     printf("%d * %d = %d",myVarA,myVarB,mySum1);
     system("pause");
     scanf("%*c");
         return 0;[/ICODE]
}

So i am stuck and this is only the first out of 3 operations and the 1st out of three operations it has to do. i have sat in the tutoring lab at school and the tutors are stumped. Just for the reference here is the statement from the assignment:
(a * a) + (a / b) + (c % a)

  • a, b, and c are INT values chosen by the user
  • The user must enter the expression with the exact parentheses, spaces, and operators shown
  • The user may enter any valid values for the variables
  • The result is an INT value

remove the commas from your scanf() line of code

I dont know why i put the comas in there, i guess i was tired. Spent 3 hours in the lab at school to no avail. Still don't work though:

please repost your most current code. please use code tags and indentation.

thanks.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main() {
  int VarA, VarB, Sum1;
  char op = '\0', ex;   //ex = extra char's
  VarA=VarB=Sum1 = 0;
  printf("Enter equation exactly as you see, inserting an INT for a, b, and c\n(a * b): \n");
  scanf("%c%d %c %d%c",&ex,&VarA,&op,&VarB,&ex);
  getchar(); //removes the newline from the keyboard buffer
  Sum1=VarA*VarB;
  printf("%d * %d = %d",VarA,VarB,Sum1);
  getchar();
  return 0; 
}

Sorry about the formatting issues, will be more vigilant in the future!
So this is where i am at, i got the single operation to work, and calculate correctly. Now even with the (char op = '\0', ex; Thank you Adak, i cant believe i forgot that ) it is now feeding me garbage for the second operation and it is returning the sum of 25
i enter (5 * 5) + (6 / 6) into the prompt every time and i get :
(5 * 5) + (2 / 21)=25
Ahhh, what am doing wrong.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main() 
{
 int myVarA,myVarB,myVarC,myVarD,myVarE,myVarF,mySum1,mySum2,mySum3;
     char a,b,c,d,e,f,g,h;
     char op = '\0', ex;
     
     printf("Enter equation exactly as you see, inserting an INT for a, b, and c\n(a * b) + (c / d): \n");
     scanf("%c  %d  %c  %d  %c %c %c",&a,&myVarA,&b,&myVarB,&c,&d,&h);
     scanf("%c  %d  %c  %d  %c",&e,&myVarC,&f,&myVarD,&g);
     mySum1=(myVarA * myVarB);
     mySum2=(myVarC / myVarD);
     mySum3=(mySum1 + mySum2);                         
     printf("(%d * %d) + (%d / %d) = %d\n",myVarA,myVarB,myVarC,myVarD,mySum3);
     system("pause");
     scanf("%*c");
     return 0;
}

well, unfortunately, the quick answer is "scanf is a terrible method to get terminal input"

the long answer is that scanf can do it properly, but in a very complex manner.

a longer answer is that fgets() or fgetc() can do it properly but in a not-quite-so-complex manner.


.

well, unfortunately, the quick answer is "scanf is a terrible method to get terminal input"

the long answer is that scanf can do it properly, but in a very complex manner.

a longer answer is that fgets() or fgetc() can do it properly but in a not-quite-so-complex manner.


.

Yeah, but we have not gone over that in class yet. However the chunk of code Adak posted is working as I add more Arithmetic opperations to it. Thank God for you guys, this class is hard as heck. Doesn't help that i haven't been in school for 10 years! Thanks guys and gals.

but i'm sure you want this thing to "work" and not worry about "long answers" :)

so here is something that works. but just barely. remove your two scanf lines and replace wiht this one:

scanf("%c%d %c %d%c %c %c%d %c %d%c",&a,&myVarA,&b,&myVarB,&c,&d,&e,&myVarC,&f,&myVarD,&g);

the reason why this will work, is that it follows your expected/anticipated whitespace verbatim.

the reason why scanf() generally sucks for getting input from a terminal, is that it follows your expected/anticipated whitespace verbatim.

tehre is a way to make scanf() robust, but it's quite complicated and i barely know how to do it much less explain it. the reason why i dont care, is becasue i always use fgets() or fgetc() for getting input from a terminal.


However the chunk of code Adak posted is working as I add more Arithmetic opperations to it.

oh i didnt see adaks code. which is just a generalized version of the longer specific code i posted.

and yes, it is "working" ... but i repeat, it is working just barely.

this code will fail, and teh program may crash spectacularly, as soon as your user mistypes an extra bit of whitespace to the terminal.

i don't consider that to be "working". but if your course instructor refuses to teach you fgets() and other IO functions until later, i guess it will do for now.


.

it is now feeding me garbage for the second operation and it is returning the sum of 25
i enter (5 * 5) + (6 / 6) into the prompt every time and i get :
(5 * 5) + (2 / 21)=25

Ahhh, what am doing wrong.

you're using integer types and expecting floating point results.

you're using integer types and expecting floating point results.

i just realized that. Thanks got it working with the "verbatim" approach. The prof said that there was a way to get it to work with fgets and all but i have 3 other classes to study for and a major test tomorrow. The help i got here was perfect and is working well now. Thank you guys alot, dont know what i would do with out this site other than replace a keyboard a week. ;)

be advised, if this is a real university course, and you've been told about fgets() you will probably lose points for using scanf() in this manner. it's very fragile as you have seen.

but i understand the full load and looming exams.

sometimes you just have to cut your losses ;P

Any one have any suggestions for this i need it to output a floating point decimal and i cant seem to get it to read or print right. This class is as tough as learning to read Russian backwards.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main() {
  int VarA1, VarB1, VarC1, VarD1, VarE1, VarF1, Sum1,VarA3, VarB3;
  float  VarA2, VarB2, VarC2, VarD2, Sum2, VarC3, VarD3, Sum3;
  char op = '\0', ex;   
  VarA1=VarB1=VarC1=VarD1=VarE1=VarF1=Sum1=VarA2=VarB2=VarC2=VarD2=Sum2=VarA3=VarB3=VarC3=VarD3=Sum3 = 0;
printf("Enter equation as stated inserting an INT for a, b, c, and d, \n(a / b) * (c / d) : \n");
  scanf("%c%d %c %d%c %c %c%lf %c %lf%c%*c",
  &ex,&VarA3,&op,&VarB3,&ex,&op,&ex,&VarC3,&op,&VarD3,&ex);
  Sum2=(VarA3-VarB3)%((int)VarC3*(int)VarD3);
  printf("(%d - %d) % (%lf * %lf) = %f",
  VarA3,VarB3,VarC3,VarD3,Sum3);

Any help would be appreciated, a good explanation would be golden. I dont just want to pass this assignment, i want to know what the hell i am doing.

Any one have any suggestions for this i need it to output a floating point decimal and i cant seem to get it to read or print right. This class is as tough as learning to read Russian backwards.

... i want to know what the hell i am doing.

goodlord, this is an effing mess.

it's no wonder you don't know what you're doing. no one can even read this. this is an illustration of what not to do. it's called "obfuscated code" and it's not a desirable thing.

i can't help you with this if you're going to continue to use scanf(). if you want to learn how to use standard function fgets() atoi() and a few others, i'll help you clean this up.

goodlord, this is an effing mess.

it's no wonder you don't know what you're doing. no one can even read this. this is an illustration of what not to do. it's called "obfuscated code" and it's not a desirable thing.

i can't help you with this if you're going to continue to use scanf(). if you want to learn how to use standard function fgets() atoi() and a few others, i'll help you clean this up.

Absolutly, we covered just a little of those and i am lost as it is. Please Hep Here is the whole assingnment and the code i have

Program lab3a.c

This program will prompt the user for 3 arithmetic expressions and display the results of each. The expressions are:

* (a * a) + (a / b) + (c % a)
o a, b, and c are INT values chosen by the user
o The user must enter the expression with the exact parentheses, spaces, and operators shown
o The user may enter any valid values for the variables
o The result is an INT value

* (a / b) * (b / a)
o a and b are FLOAT values chosen by the user
o The user must enter the expression with the exact parentheses, spaces, and operators shown
o The user may enter any valid values for the variables
o The resuld should be displayed with 3 decimal points of precision

* (a - b) % (int)(c * d)
o a and b are INT values chosen by the user
o c and d are FLOAT values chosen by the uer
o The user must enter the expression with the exact parentheses, spaces, and operators shown
o The user may enter any valid values for the variables
o The result is a FLOAT value

Your program must read the operators, parentheses, and values in the expressions, and then display the result. Here is an example of how the program dialog should look:

Enter '(int * int) + (int / int) + (int % int)': (12 * 5) + (12 / 5) + (1 % 5)
(12 * 5) + (12 / 5) + (1 % 5) = 63

Enter '(float / float) * (float / float)': (1.54 / 0.2313) * (-3.5234 / 0.1232)
(1.540 / 0.231) * (-3.523 / 0.123) = -190.413

Enter '(int - int) % (float * float)': (10 - 4) % (4.15 * 6.54)
(10 - 4) % (4.150000 * 6.540000) = 6

/*Thomas EK
G03353540
lab3a.c
Recieved help from Adak on Daniweb with the Char op, Var = 0 and the getchar () comands */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main() {
  int VarA1, VarB1, VarC1, VarD1, VarE1, VarF1, Sum1,VarA3, VarB3;
  float  VarA2, VarB2, VarC2, VarD2, Sum2, VarC3, VarD3, Sum3, Remainder2;
  char op = '\0', ex;   
  VarA1=VarB1=VarC1=VarD1=VarE1=VarF1=Sum1=VarA2=VarB2=VarC2=VarD2=Sum2=VarA3=VarB3=VarC3=VarD3=Sum3=Remainder2 = 0;
  printf("Enter equation as stated inserting an INT for a, b, c, d, e, and f:\n(a * b) + (c / d) + (e %% f):\n");
  scanf("%c%d %c %d%c %c %c%d %c %d%c %c %c%d %c %d%c%*c",&ex,&VarA1,&op,&VarB1,&ex,&op,&ex,&VarC1,&op,&VarD1,&ex,&ex,&ex,&VarE1,&op,&VarF1,&ex);
  Sum1=(VarA1*VarB1+VarC1/VarD1+VarE1%VarF1);
  printf("((%d * %d) + (%d / %d) + (%d %% %d)) = %d\nPress Enter...",VarA1,VarB1,VarC1,VarD1,VarE1,VarF1,Sum1);
  scanf("%*c");
  
  printf("Enter equation as stated inserting an INT for a, b, c, and d: \n(a / b) * (c / d) :\n ");
  scanf("%c%f %c %f%c %c %c%f %c %f%c%*c",
  &ex,&VarA2,&op,&VarB2,&ex,&op,&ex,&VarC2,&op,&VarD2,&ex);
  Sum2=(VarA2/VarB2*VarC2/VarD2);
  printf("(%.3f / %.3f) * (%.3f / %.3f) = %.3f\nPress Enter....",VarA2,VarB2,VarC2,VarD2,Sum2);
  scanf("%*c");
  
  printf("Enter equation as stated inserting an INT for a, b, c, and d,\n(a / b) * (c / d):");
  scanf("%c%d %c %d%c %c %c%f %c %f%c%*c",&ex,&VarA3,&op,&VarB3,&ex,&op,&ex,&VarC3,&op,&VarD3,&ex);
  Sum2=(VarA3-VarB3)%(VarC3*VarD3);
  printf("(%d - %d) %% (%f * %f) = %.f",VarA3,VarB3,VarC3,VarD3,Sum3,Remainder2);
  scanf("%*c");
  
  

  return 0; 
}

My word, Thomas! You're running amok on us! ;)

Please set up your printed prompts for the user, just like the assignment asks for:
(one example):
Enter '(int * int) + (int / int) + (int % int)':

No reason to be knocked off on points for missing some gimme's.

In these example's there's only two numbers that need to be dealt with at a time, so I think you have a few too many variables, but I'll work up a little example, and see if my idea is right or not before suggesting it to you.

Using fgets and atoi is a good idea.

talk to you soon

My word, Thomas! You're running amok on us! ;)

Please set up your printed prompts for the user, just like the assignment asks for:
(one example):
Enter '(int * int) + (int / int) + (int % int)':

No reason to be knocked off on points for missing some gimme's.

In these example's there's only two numbers that need to be dealt with at a time, so I think you have a few too many variables, but I'll work up a little example, and see if my idea is right or not before suggesting it to you.

Using fgets and atoi is a good idea.

talk to you soon

Yeah, the gimmies are not so important when the code dont do what it is supposed to. That is my main focus. I was just copying and pasting the math to the cmd and running it. Touch up comes after the walls are built kind of thing. I am unsure of how to do it with fgets and atoi. He mentioned it but i am not sure how to implament it. This is the hardest class i have, good thing he stated that the class was more about research and completion of assignments than anything.

hey man, since your requirements specify that the user must enter the equation exactly as shown, then scanf() will be sufficient to obey the letter of the assignment.

you'll get to the rest later, for now, scanf is the quick way.

consider the case for the equation ( a + b ) / ( c * d ) .... here's how you'd implement it using scanf.

#include <stdio.h>

int main (void)
{
    int a, b, c, d, e, f, status, expected;
    double result;
    
    printf("enter equation exactly as shown:\n( a + b ) / ( c * d )\n");
    
    expected = 4;
    status = scanf("( %d + %d ) / ( %d * %d )", &a, &b, &c, &d);
    
    if (status != expected)
        printf("\ninvalid input, status = %d\n", status);
        
    else 
    {
        result = ( a + b ) / (double)( c * d );
        printf("\n( %d + %d ) / ( %d * %d ) = %f\n", a, b, c, d, result);
    }
    
    return 0;
}

now follow this format and apply it ot the rest of your assignment.

I'm making one that uses strings, but it's not nearly as easy as using scanf() and numbers.

I have everything but the last section working as it is supposed to. The problem i am having is the float input and output. I have to caste it as an int so it will run the operation but i keep getting 0.000000 for an answer. Doesn't %= the remainder of an operation with math.h or is there something i am missing? I would love to use fgets but i haven't a practacal working knoledge of the steps to get a functional chunk of code that will work for what i am doing. The user has to be able to chose any number they want as long as it is either an int of float, and i tried japhthah's using randon input that fit the gimmie and i keep getting a return error.

int main() {
  int VarA1, VarB1, VarC1, VarD1, VarE1, VarF1, Sum1,VarA3, VarB3;
  float  VarA2, VarB2, VarC2, VarD2, Sum2, VarC3, VarD3, Sum3;
  char op = '\0', ex;   //ex = extra char's
  VarA1=VarB1=VarC1=VarD1=VarE1=VarF1=Sum1=VarA2=VarB2=VarC2=VarD2=Sum2=VarA3=VarB3=VarC3=VarD3=Sum3 = 0;
printf("Enter equation as stated inserting an INT for a, b, c, and d, \n(a / b) * (c / d) : \n");
  scanf("%c%d %c %d%c %c %c%f %c %f%c%*c",&ex,&VarA3,&op,&VarB3,&ex,&op,&ex,&VarC3,&op,&VarD3,&ex);
  getchar(); //removes the newline from the keyboard buffer
  Sum2=(VarA3/VarB3*VarC3/VarD3);
  printf("%d / %d * %f / %f = %f",VarA3,VarB3,VarC3,VarD3,Sum3);
  getchar();
  scanf("%*c");

look this is just nuts, man.

now just stop what you're doing. seriously. stop. it's an unintelligible mess and painful to look at. C code is not supposed to look like that. it's an abomination.

for one thing, you don't need a variable for every single parenthesis and math operator. look at the the example i posted.

#include <stdio.h>

int main (void)
{
    int a, b, c, d;
    int inputsFound, inputsExpected;
    double result;
    
    printf("enter equation exactly as shown:\n");
    printf("( a + b ) / ( c * d )\n");
    
    /* should find four (4) variable from scan */
    inputsExpected = 4;   
    inputsFound = scanf("( %d + %d ) / ( %d * %d )", &a, &b, &c, &d);
    
    if (inputsFound != inputsExpected)
        printf("\ninvalid number of inputs found = %d\n", inputsFound);
        
    else 
    {
        result = ( a + b ) / (double)( c * d );
        printf("\n( %d + %d ) / ( %d * %d ) = %f\n", a, b, c, d, result);
    }
    
    return 0;
}

see how clear and coherent this is? now do yours like this.

look this is just nuts, man.

now just stop what you're doing. seriously. stop. it's an unintelligible mess and painful to look at. C code is not supposed to look like that. it's an abomination.

for one thing, you don't need a variable for every single parenthesis and math operator. look at the the example i posted.

#include <stdio.h>

int main (void)
{
    int a, b, c, d;
    int inputsFound, inputsExpected;
    double result;
    
    printf("enter equation exactly as shown:\n");
    printf("( a + b ) / ( c * d )\n");
    
    /* should find four (4) variable from scan */
    finputsExpected = 4;   
    inputsFound = scanf("( %d + %d ) / ( %d * %d )", &a, &b, &c, &d);
    
    if (inputsFound != inputsExpected)
        printf("\ninvalid number of inputs found = %d\n", inputsFound);
        
    else 
    {
        result = ( a + b ) / (double)( c * d );
        printf("\n( %d + %d ) / ( %d * %d ) = %f\n", a, b, c, d, result);
    }
    
    return 0;
}

see how clear and coherent this is? now do yours like this.

Here is the error message i got from just compiling
`finputsExpected' undeclared (first use this function)

sorry, that line #12 had a typo. it should be

inputsExpected = 4;

i corrected it in the post above. change yours to just remove that "f" at the beginning of the finputsExpected

sorry for the confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.