The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
0 failures but does not complile. Does anyone know what is happening?
Discuss 0 failures but does not complile. Does anyone know what is happening? in the C Programming forum on Dev Shed. 0 failures but does not complile. Does anyone know what is happening? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 20th, 2012, 10:20 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 1
Time spent in forums: 17 m 15 sec
Reputation Power: 0
|
|
|
0 failures but does not complile. Does anyone know what is happening?
Hello I am writing a calculator program the output should be:
1. addition
2. subtraction
3. multiplicaiton
4. division
5. Exit
please select an operand?
please enter two numbers separated by a space: ?
the result of %f and % f is %f.
I've tried while but something is wrong with it. Any help I would be so grateful. I cant attach the code below so i copied pasted it:
#include<stdio.h>
float choice;
float result;
float num1, num2;
int get_choice();
int main()
{
{
float num1, num2;
int choice;
float result;
do
{
printf("Welcome to Colleen Gonzales's Handy Calculator");
printf("1. Addition\n");
printf("2. Subtract\n");
printf("3. Multipilcation\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("\n\nPlease pick an operation.\n");
scanf("%f", &choice);
}
while( (choice = 1,2,3, 4 , 5) );
printf("Please choose two numbers seperated by a space:");
scanf("%f %f", &num1, &num2);
return 0;
}
{
float num1, num2, result;
int choice;
while( (choice=get_choice()) !=5)
{
switch (choice)
{
case 1 : result = num1 + num2;
break;
case 2 : result = num1-num2;
break;
case 3 : result = num1 * num2;
break;
case 4 : result = num1/num2;
break;
case 5 : printf("Goodbye");
break;
default  rintf("Please respond with 1, 2, 3, 4, or 5\n");
break; /*end of switch*/
}
}
printf("The result of %f and %f is %.2f\n", &num1, & num2, &result);
return 0;
}
}
|

August 21st, 2012, 12:01 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
What do you mean by "failures"? Do you mean error messages?
Well, what warnings did you get? Here's what I got when I compiled it using MinGW gcc in a Win7 console (AKA "command line"):
Quote: C:>gcc -Wall quoi.c
quoi.c: In function `main':
quoi.c:23: warning: float format, different type arg (arg 2)
quoi.c:25: warning: left-hand operand of comma expression has no effect
quoi.c:25: warning: left-hand operand of comma expression has no effect
quoi.c:25: warning: left-hand operand of comma expression has no effect
quoi.c:11: warning: unused variable `result'
quoi.c:57: warning: double format, pointer arg (arg 2)
quoi.c:57: warning: double format, pointer arg (arg 3)
quoi.c:57: warning: double format, pointer arg (arg 4)
C:AppData\Local\Temp\ccWqeaaa.o(.text+0x219):quoi.c: undefined reference to `get_choice'
C:> |
Are you ignoring warnings? Bad idea! Warnings are much more important than error messages; never ignore them unless you absolutely positively know you safely can, and even then double-check! Warnings mean that the compiler doesn't understand what you're writing and is confused. A confused compiler can be a very dangerous animal, because the executable that it creates is virtually guaranteed to do things quite different than what you wanted.
Also use code tags! Here's what you code looks like when you preserve the indentation:
Code:
#include<stdio.h>
float choice;
float result;
float num1, num2;
int get_choice();
int main()
{
{
float num1, num2;
int choice;
float result;
do
{
printf("Welcome to Colleen Gonzales's Handy Calculator");
printf("1. Addition\n");
printf("2. Subtract\n");
printf("3. Multipilcation\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("\n\nPlease pick an operation.\n");
scanf("%f", &choice);
}
while( (choice = 1,2,3, 4 , 5) );
printf("Please choose two numbers seperated by a space:");
scanf("%f %f", &num1, &num2);
return 0;
}
{
float num1, num2, result;
int choice;
while( (choice=get_choice()) !=5)
{
switch (choice)
{
case 1 : result = num1 + num2;
break;
case 2 : result = num1-num2;
break;
case 3 : result = num1 * num2;
break;
case 4 : result = num1/num2;
break;
case 5 : printf("Goodbye");
break;
default :printf("Please respond with 1, 2, 3, 4, or 5\n");
break; /*end of switch*/
}
}
printf("The result of %f and %f is %.2f\n", &num1, & num2, &result);
return 0;
}
}
Looks like what you had written, right? Well, that's another problem. With all due respect, that is some of the worst "formatting" that I've seen. And your poor indenting style is hiding a big problem which just jumps right out at you when it's indented properly:
Code:
#include<stdio.h>
float choice;
float result;
float num1, num2;
int get_choice();
int main()
{
{
float num1, num2;
int choice;
float result;
do
{
printf("Welcome to Colleen Gonzales's Handy Calculator");
printf("1. Addition\n");
printf("2. Subtract\n");
printf("3. Multipilcation\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("\n\nPlease pick an operation.\n");
scanf("%f", &choice);
}
while( (choice = 1,2,3, 4 , 5) );
printf("Please choose two numbers seperated by a space:");
scanf("%f %f", &num1, &num2);
return 0;
}
{
float num1, num2, result;
int choice;
while( (choice=get_choice()) !=5)
{
switch (choice)
{
case 1 : result = num1 + num2;
break;
case 2 : result = num1-num2;
break;
case 3 : result = num1 * num2;
break;
case 4 : result = num1/num2;
break;
case 5 : printf("Goodbye");
break;
default: printf("Please respond with 1, 2, 3, 4, or 5\n");
break; /*end of switch*/
}
}
printf("The result of %f and %f is %.2f\n", &num1, & num2, &result);
return 0;
}
}
Right off, you can see that you're missing a close brace. That's what proper and consistent indenting can do for you! Learn it! Use it! Stop shooting yourself in the foot by hiding stupid mistakes from yourself!
What the hell are you trying to do there? Why did you have those extra levels of code blocks (delimited by the braces ( { } ))? And what's that second code block about? Was that supposed to be the get_choice() function that you prototyped but then never ever created? If you need to review how to declare a function, then I recommend that you do so.
Then there's this line that you were warned about :
while( (choice = 1,2,3, 4 , 5) );
quoi.c:25: warning: left-hand operand of comma expression has no effect
quoi.c:25: warning: left-hand operand of comma expression has no effect
quoi.c:25: warning: left-hand operand of comma expression has no effect
What you are doing with the statement is to assign a value of 5 to choice, but only after evaluating four other expressions which never did anything (those are the 1, 2, 3, 4). Were you instead wanting to test for choice being equal to 1 or 2 or 3 or 4? If so, then why didn't you write that?
Remember: = means assignment, whereas == means testing for equality. They're two different things!
Each test needs to be done as a separate expression and they can then all be OR'ed together, so that should instead be:
Code:
while(choice == 1 || choice == 2 || choice == 3 ||
choice == 4 || choice == 5) );
|

August 21st, 2012, 10:07 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 2
Time spent in forums: 38 m 25 sec
Reputation Power: 0
|
|
|
Hope this helps
There are a few run-time errors.
You may want to decide whether you want to use Global or pass by value.
In the below I have used pass by value.
Hope it helps.
====================================================
#include<stdio.h>
//==>rasbv:Removed unwanted variables.
//int choice;
//float result;
//float num1, num2;
//rasbv: note I chaged the return to be float as the result will be in float
//rasbv: add to argument list using by value here
float get_choice(float num1, float num2, int choice);
//==>rasbv:Note that your "copy paste" have errors on brackets
int main()
{
int choice;
float result;
float num1, num2;
char buff[256];
do
{
printf("Welcome to Colleen Gonzales's Handy Calculator\n\n");
printf("\n\nPlease pick an operation.\n");
printf("1. Addition\n");
printf("2. Subtract\n");
printf("3. Multipilcation\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("\n\nYour choice is:");
scanf("%d", &choice);
flushall();
if((choice<=4) && (choice>=1))
{
//rasbv: Ask for the 2 numbers inside the loop
printf("Please choose two numbers seperated by a space:\n");
fgets(buff, 256, stdin); //<-- rasbv:use fgets to read the whole line and later sscanf to get the 2 numbers
if(sscanf(buff, "%f %f", &num1, &num2)==2)
{
result=get_choice(num1, num2, choice); //<-rasbv: call the function
}
else
{
printf("Error in sscanf\n\n");
}
}
}
while( (choice<=4)&&(choice>=1));//<-rasbv: change the condition to be between 1 to 4
printf("\n\nThank you for using.\nPlease come again  \n Tata!!\n");
return 0;
}
float get_choice(float num1, float num2, int choice)
{
//==>rasbv:Removed unwanted variables.
float result;
switch (choice)
{
case 1 : result = num1 + num2;
break;
case 2 : result = num1-num2;
break;
case 3 : result = num1 * num2;
break;
case 4 : result = num1/num2;
break;
case 5 : printf("Goodbye");
break;
default: printf("Please respond with 1, 2, 3, 4, or 5\n");
break; /*end of switch*/
}
printf("The result of %.4f and %.4f is %.2f\n", num1, num2, result);//<--rasbv: no & here
return result;
}
====================================================
Cheers
Ras
|

August 21st, 2012, 10:13 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 2
Time spent in forums: 38 m 25 sec
Reputation Power: 0
|
|
Sorry. Re-post with code tag.
There are a few run-time errors.
You may want to decide whether you want to use Global or pass by value.
In the below I have used pass by value.
Hope it helps.
Code:
#include<stdio.h>
//==>rasbv:Removed unwanted variables.
//int choice;
//float result;
//float num1, num2;
//rasbv: note I chaged the return to be float as the result will be in float
//rasbv: add to argument list using by value here
float get_choice(float num1, float num2, int choice);
//==>rasbv:Note that your "copy paste" have errors on brackets
int main()
{
int choice;
float result;
float num1, num2;
char buff[256];
do
{
printf("Welcome to Colleen Gonzales's Handy Calculator\n\n");
printf("\n\nPlease pick an operation.\n");
printf("1. Addition\n");
printf("2. Subtract\n");
printf("3. Multipilcation\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("\n\nYour choice is:");
scanf("%d", &choice);
flushall();
if((choice<=4) && (choice>=1))
{
//rasbv: Ask for the 2 numbers inside the loop
printf("Please choose two numbers seperated by a space:\n");
fgets(buff, 256, stdin); //<-- rasbv:use fgets to read the whole line and later sscanf to get the 2 numbers
if(sscanf(buff, "%f %f", &num1, &num2)==2)
{
result=get_choice(num1, num2, choice); //<-rasbv: call the function
}
else
{
printf("Error in sscanf\n\n");
}
}
}
while( (choice<=4)&&(choice>=1));//<-rasbv: change the condition to be between 1 to 4
printf("\n\nThank you for using.\nPlease come again :)\n Tata!!\n");
return 0;
}
float get_choice(float num1, float num2, int choice)
{
//==>rasbv:Removed unwanted variables.
float result;
switch (choice)
{
case 1 : result = num1 + num2;
break;
case 2 : result = num1-num2;
break;
case 3 : result = num1 * num2;
break;
case 4 : result = num1/num2;
break;
case 5 : printf("Goodbye");
break;
default: printf("Please respond with 1, 2, 3, 4, or 5\n");
break; /*end of switch*/
}
printf("The result of %.4f and %.4f is %.2f\n", num1, num2, result);//<--rasbv: no & here
return result;
}
Cheers
Ras
|

August 22nd, 2012, 07:58 AM
|
 |
Contributing User
|
|
|
|
Here's a slightly improved version. With your flushall elided, I inserted a loop to dispose of the new line character before the call to fgets. Use ncurses if you need unbuffered terminal io.
Code:
#include<stdio.h>
void do_the_computation(float num1, float num2, int choice) {
float result;
char*operator;
switch (choice) {
case 1 : result = num1 + num2;operator = "plus";
break; case 2 : result = num1-num2; operator = "minus";
break; case 3 : result = num1 * num2;operator = "times";
break; case 4 : result = num1 / num2;operator = "divided by";
break; case 5 : fputs("Goodbye",stdout); /* never happens because you prescreened choice */
break; default: puts("Please respond with 1, 2, 3, 4, or 5"); /* never happens because you prescreened choice */
break; /*end of switch*/
}
printf("The result of %.4f %s %.4f is %.2f\n", num1, operator, num2, result);
}
int main() {
int choice;
float num1, num2;
char buff[256];
puts("Welcome to Colleen Gonzales's Handy Calculator\n");
do {
puts("\n\nPlease pick an operation.");
puts("1. Addition");
puts("2. Subtract");
puts("3. Multiplication");
puts("4. Division");
puts("5. Exit");
fputs("\n\nYour choice is:",stdout);
scanf("%d", &choice);
while('\n' != getchar())
;
if (5 == choice)
choice = 0;
else if ((choice < 1) || (4 < choice))
choice = 1;
else {
puts("Please choose two numbers seperated by a space:");
fgets(buff, 256, stdin);
if(2 == sscanf(buff, "%f %f", &num1, &num2))
do_the_computation(num1, num2, choice);
else
puts("Error in sscanf\n");
}
} while(choice);
puts("\n\nThank you for using.\nPlease come again :)\n Tata!!");
return 0;
}
__________________
[code] Code tags[/code] are essential for python code!
|

August 25th, 2012, 10:23 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Location: Subhasgram,Kolkata,WB,IND
Posts: 13
Time spent in forums: 4 h 20 m 4 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by dwise1_aol
Code:
while(choice == 1 || choice == 2 || choice == 3 ||
choice == 4 || choice == 5) );
|
It's better to keep it easy
Code:
while(choice>0&&choice<6)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|