How to debug a C program producing unexpected output?
up vote
-2
down vote
favorite
I am new to programming C and I don't know why my program is not printing the desired output.
#include <stdio.h>
void main(void)
{
char res,res1;
float money=10;
printf("***Wealcome to Peace of Mind***");
printf("nHello we have the menu please check::");
printf("n***Menú***");
printf("n");
printf("n<<<Bebidas>>>");
printf("n 1 - Coca-Cola = 1,5 2 - IceTea = 1,4");
printf("n 3 - Super Bock = 1,70 4 - Sumol = 1,6");
printf("n");
scanf("%d",&res);
switch(res)
{
case 1 || 'Coca-Cola':money - CocaCola;break;
}
printf("%.1f",money);
//Is that result i want:
printf("n%.1f",10-1.5);
}
Output of my program:
c
add a comment |
up vote
-2
down vote
favorite
I am new to programming C and I don't know why my program is not printing the desired output.
#include <stdio.h>
void main(void)
{
char res,res1;
float money=10;
printf("***Wealcome to Peace of Mind***");
printf("nHello we have the menu please check::");
printf("n***Menú***");
printf("n");
printf("n<<<Bebidas>>>");
printf("n 1 - Coca-Cola = 1,5 2 - IceTea = 1,4");
printf("n 3 - Super Bock = 1,70 4 - Sumol = 1,6");
printf("n");
scanf("%d",&res);
switch(res)
{
case 1 || 'Coca-Cola':money - CocaCola;break;
}
printf("%.1f",money);
//Is that result i want:
printf("n%.1f",10-1.5);
}
Output of my program:
c
3
What is your desired output?
– Tobias Wilfert
Nov 19 at 22:57
1
case 1 || 'Coca-Cola':money - CocaCola;break;
<-- what are you trying to do there?
– Govind Parmar
Nov 19 at 22:58
Minor point of technique: please output the newline at the end of each line, not at the beginning.
– Weather Vane
Nov 19 at 23:14
The compiler should warn you about the wrong variable type inscanf("%d",&res);
.
– Weather Vane
Nov 19 at 23:15
1
Most of this program is broken. I recommend starting with a good introductory book on C, and work through the exercises until you have a handle on the basics.
– paddy
Nov 19 at 23:28
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I am new to programming C and I don't know why my program is not printing the desired output.
#include <stdio.h>
void main(void)
{
char res,res1;
float money=10;
printf("***Wealcome to Peace of Mind***");
printf("nHello we have the menu please check::");
printf("n***Menú***");
printf("n");
printf("n<<<Bebidas>>>");
printf("n 1 - Coca-Cola = 1,5 2 - IceTea = 1,4");
printf("n 3 - Super Bock = 1,70 4 - Sumol = 1,6");
printf("n");
scanf("%d",&res);
switch(res)
{
case 1 || 'Coca-Cola':money - CocaCola;break;
}
printf("%.1f",money);
//Is that result i want:
printf("n%.1f",10-1.5);
}
Output of my program:
c
I am new to programming C and I don't know why my program is not printing the desired output.
#include <stdio.h>
void main(void)
{
char res,res1;
float money=10;
printf("***Wealcome to Peace of Mind***");
printf("nHello we have the menu please check::");
printf("n***Menú***");
printf("n");
printf("n<<<Bebidas>>>");
printf("n 1 - Coca-Cola = 1,5 2 - IceTea = 1,4");
printf("n 3 - Super Bock = 1,70 4 - Sumol = 1,6");
printf("n");
scanf("%d",&res);
switch(res)
{
case 1 || 'Coca-Cola':money - CocaCola;break;
}
printf("%.1f",money);
//Is that result i want:
printf("n%.1f",10-1.5);
}
Output of my program:
c
c
edited Nov 21 at 21:56
halfer
14.2k758106
14.2k758106
asked Nov 19 at 22:55
Kingu39
62
62
3
What is your desired output?
– Tobias Wilfert
Nov 19 at 22:57
1
case 1 || 'Coca-Cola':money - CocaCola;break;
<-- what are you trying to do there?
– Govind Parmar
Nov 19 at 22:58
Minor point of technique: please output the newline at the end of each line, not at the beginning.
– Weather Vane
Nov 19 at 23:14
The compiler should warn you about the wrong variable type inscanf("%d",&res);
.
– Weather Vane
Nov 19 at 23:15
1
Most of this program is broken. I recommend starting with a good introductory book on C, and work through the exercises until you have a handle on the basics.
– paddy
Nov 19 at 23:28
add a comment |
3
What is your desired output?
– Tobias Wilfert
Nov 19 at 22:57
1
case 1 || 'Coca-Cola':money - CocaCola;break;
<-- what are you trying to do there?
– Govind Parmar
Nov 19 at 22:58
Minor point of technique: please output the newline at the end of each line, not at the beginning.
– Weather Vane
Nov 19 at 23:14
The compiler should warn you about the wrong variable type inscanf("%d",&res);
.
– Weather Vane
Nov 19 at 23:15
1
Most of this program is broken. I recommend starting with a good introductory book on C, and work through the exercises until you have a handle on the basics.
– paddy
Nov 19 at 23:28
3
3
What is your desired output?
– Tobias Wilfert
Nov 19 at 22:57
What is your desired output?
– Tobias Wilfert
Nov 19 at 22:57
1
1
case 1 || 'Coca-Cola':money - CocaCola;break;
<-- what are you trying to do there?– Govind Parmar
Nov 19 at 22:58
case 1 || 'Coca-Cola':money - CocaCola;break;
<-- what are you trying to do there?– Govind Parmar
Nov 19 at 22:58
Minor point of technique: please output the newline at the end of each line, not at the beginning.
– Weather Vane
Nov 19 at 23:14
Minor point of technique: please output the newline at the end of each line, not at the beginning.
– Weather Vane
Nov 19 at 23:14
The compiler should warn you about the wrong variable type in
scanf("%d",&res);
.– Weather Vane
Nov 19 at 23:15
The compiler should warn you about the wrong variable type in
scanf("%d",&res);
.– Weather Vane
Nov 19 at 23:15
1
1
Most of this program is broken. I recommend starting with a good introductory book on C, and work through the exercises until you have a handle on the basics.
– paddy
Nov 19 at 23:28
Most of this program is broken. I recommend starting with a good introductory book on C, and work through the exercises until you have a handle on the basics.
– paddy
Nov 19 at 23:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The syntax of your case
statement is not correct. Also the code is using scanf()
to read an integer, but storing the integer-sized value in a char
.
I tidied up the code:
#include <stdio.h>
int main(void)
{
int res;
float cost = 0;
float money = 10;
printf("***Wealcome to Peace of Mind***n");
printf("Hello we have the menu please check::n");
printf("***Menú***n");
printf("n");
printf("<<<Bebidas>>>n");
printf(" 1 - Coca-Cola = 1,5 2 - IceTea = 1,4n");
printf(" 3 - Super Bock = 1,70 4 - Sumol = 1,6n");
printf("n");
scanf("%d", &res);
switch(res)
{
case 1:
cost = 1.5;
break;
case 2:
cost = 1.4;
break;
// TODO: case 3 & 4
default:
printf("Invalid Entryn");
cost = 0;
}
printf("money = %.1fn", money - cost);
return 0;
}
Some further notes:
- As commentators pointed out, put
n
at the end of the string - When you compile, turn on warnings, and try to fix all of them.
- In
case
blocks, it's good practice to have adefault
to catch errors - It would be worthwhile to store the drink prices as
#define
constants (or as an array of values, or some common area so the value is only set once in the program, and everything else just references that.)
#define COLA_COST 1.5
Thanks for helping me first, I'll use the tips you gave me and note down to remember. I just have difficulty using the main int (void) I've always used the void main (void) so I'll search better now on that I greet the help.
– Kingu39
Nov 20 at 23:08
Theint main(void)
just allows the code to return an exit code. It's the convention to return0
for OK, and some other number to indicate an error-type.
– Kingsley
Nov 20 at 23:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The syntax of your case
statement is not correct. Also the code is using scanf()
to read an integer, but storing the integer-sized value in a char
.
I tidied up the code:
#include <stdio.h>
int main(void)
{
int res;
float cost = 0;
float money = 10;
printf("***Wealcome to Peace of Mind***n");
printf("Hello we have the menu please check::n");
printf("***Menú***n");
printf("n");
printf("<<<Bebidas>>>n");
printf(" 1 - Coca-Cola = 1,5 2 - IceTea = 1,4n");
printf(" 3 - Super Bock = 1,70 4 - Sumol = 1,6n");
printf("n");
scanf("%d", &res);
switch(res)
{
case 1:
cost = 1.5;
break;
case 2:
cost = 1.4;
break;
// TODO: case 3 & 4
default:
printf("Invalid Entryn");
cost = 0;
}
printf("money = %.1fn", money - cost);
return 0;
}
Some further notes:
- As commentators pointed out, put
n
at the end of the string - When you compile, turn on warnings, and try to fix all of them.
- In
case
blocks, it's good practice to have adefault
to catch errors - It would be worthwhile to store the drink prices as
#define
constants (or as an array of values, or some common area so the value is only set once in the program, and everything else just references that.)
#define COLA_COST 1.5
Thanks for helping me first, I'll use the tips you gave me and note down to remember. I just have difficulty using the main int (void) I've always used the void main (void) so I'll search better now on that I greet the help.
– Kingu39
Nov 20 at 23:08
Theint main(void)
just allows the code to return an exit code. It's the convention to return0
for OK, and some other number to indicate an error-type.
– Kingsley
Nov 20 at 23:35
add a comment |
up vote
2
down vote
accepted
The syntax of your case
statement is not correct. Also the code is using scanf()
to read an integer, but storing the integer-sized value in a char
.
I tidied up the code:
#include <stdio.h>
int main(void)
{
int res;
float cost = 0;
float money = 10;
printf("***Wealcome to Peace of Mind***n");
printf("Hello we have the menu please check::n");
printf("***Menú***n");
printf("n");
printf("<<<Bebidas>>>n");
printf(" 1 - Coca-Cola = 1,5 2 - IceTea = 1,4n");
printf(" 3 - Super Bock = 1,70 4 - Sumol = 1,6n");
printf("n");
scanf("%d", &res);
switch(res)
{
case 1:
cost = 1.5;
break;
case 2:
cost = 1.4;
break;
// TODO: case 3 & 4
default:
printf("Invalid Entryn");
cost = 0;
}
printf("money = %.1fn", money - cost);
return 0;
}
Some further notes:
- As commentators pointed out, put
n
at the end of the string - When you compile, turn on warnings, and try to fix all of them.
- In
case
blocks, it's good practice to have adefault
to catch errors - It would be worthwhile to store the drink prices as
#define
constants (or as an array of values, or some common area so the value is only set once in the program, and everything else just references that.)
#define COLA_COST 1.5
Thanks for helping me first, I'll use the tips you gave me and note down to remember. I just have difficulty using the main int (void) I've always used the void main (void) so I'll search better now on that I greet the help.
– Kingu39
Nov 20 at 23:08
Theint main(void)
just allows the code to return an exit code. It's the convention to return0
for OK, and some other number to indicate an error-type.
– Kingsley
Nov 20 at 23:35
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The syntax of your case
statement is not correct. Also the code is using scanf()
to read an integer, but storing the integer-sized value in a char
.
I tidied up the code:
#include <stdio.h>
int main(void)
{
int res;
float cost = 0;
float money = 10;
printf("***Wealcome to Peace of Mind***n");
printf("Hello we have the menu please check::n");
printf("***Menú***n");
printf("n");
printf("<<<Bebidas>>>n");
printf(" 1 - Coca-Cola = 1,5 2 - IceTea = 1,4n");
printf(" 3 - Super Bock = 1,70 4 - Sumol = 1,6n");
printf("n");
scanf("%d", &res);
switch(res)
{
case 1:
cost = 1.5;
break;
case 2:
cost = 1.4;
break;
// TODO: case 3 & 4
default:
printf("Invalid Entryn");
cost = 0;
}
printf("money = %.1fn", money - cost);
return 0;
}
Some further notes:
- As commentators pointed out, put
n
at the end of the string - When you compile, turn on warnings, and try to fix all of them.
- In
case
blocks, it's good practice to have adefault
to catch errors - It would be worthwhile to store the drink prices as
#define
constants (or as an array of values, or some common area so the value is only set once in the program, and everything else just references that.)
#define COLA_COST 1.5
The syntax of your case
statement is not correct. Also the code is using scanf()
to read an integer, but storing the integer-sized value in a char
.
I tidied up the code:
#include <stdio.h>
int main(void)
{
int res;
float cost = 0;
float money = 10;
printf("***Wealcome to Peace of Mind***n");
printf("Hello we have the menu please check::n");
printf("***Menú***n");
printf("n");
printf("<<<Bebidas>>>n");
printf(" 1 - Coca-Cola = 1,5 2 - IceTea = 1,4n");
printf(" 3 - Super Bock = 1,70 4 - Sumol = 1,6n");
printf("n");
scanf("%d", &res);
switch(res)
{
case 1:
cost = 1.5;
break;
case 2:
cost = 1.4;
break;
// TODO: case 3 & 4
default:
printf("Invalid Entryn");
cost = 0;
}
printf("money = %.1fn", money - cost);
return 0;
}
Some further notes:
- As commentators pointed out, put
n
at the end of the string - When you compile, turn on warnings, and try to fix all of them.
- In
case
blocks, it's good practice to have adefault
to catch errors - It would be worthwhile to store the drink prices as
#define
constants (or as an array of values, or some common area so the value is only set once in the program, and everything else just references that.)
#define COLA_COST 1.5
answered Nov 20 at 0:53
Kingsley
1,7961918
1,7961918
Thanks for helping me first, I'll use the tips you gave me and note down to remember. I just have difficulty using the main int (void) I've always used the void main (void) so I'll search better now on that I greet the help.
– Kingu39
Nov 20 at 23:08
Theint main(void)
just allows the code to return an exit code. It's the convention to return0
for OK, and some other number to indicate an error-type.
– Kingsley
Nov 20 at 23:35
add a comment |
Thanks for helping me first, I'll use the tips you gave me and note down to remember. I just have difficulty using the main int (void) I've always used the void main (void) so I'll search better now on that I greet the help.
– Kingu39
Nov 20 at 23:08
Theint main(void)
just allows the code to return an exit code. It's the convention to return0
for OK, and some other number to indicate an error-type.
– Kingsley
Nov 20 at 23:35
Thanks for helping me first, I'll use the tips you gave me and note down to remember. I just have difficulty using the main int (void) I've always used the void main (void) so I'll search better now on that I greet the help.
– Kingu39
Nov 20 at 23:08
Thanks for helping me first, I'll use the tips you gave me and note down to remember. I just have difficulty using the main int (void) I've always used the void main (void) so I'll search better now on that I greet the help.
– Kingu39
Nov 20 at 23:08
The
int main(void)
just allows the code to return an exit code. It's the convention to return 0
for OK, and some other number to indicate an error-type.– Kingsley
Nov 20 at 23:35
The
int main(void)
just allows the code to return an exit code. It's the convention to return 0
for OK, and some other number to indicate an error-type.– Kingsley
Nov 20 at 23:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383823%2fhow-to-debug-a-c-program-producing-unexpected-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What is your desired output?
– Tobias Wilfert
Nov 19 at 22:57
1
case 1 || 'Coca-Cola':money - CocaCola;break;
<-- what are you trying to do there?– Govind Parmar
Nov 19 at 22:58
Minor point of technique: please output the newline at the end of each line, not at the beginning.
– Weather Vane
Nov 19 at 23:14
The compiler should warn you about the wrong variable type in
scanf("%d",&res);
.– Weather Vane
Nov 19 at 23:15
1
Most of this program is broken. I recommend starting with a good introductory book on C, and work through the exercises until you have a handle on the basics.
– paddy
Nov 19 at 23:28