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:



Screen Shot










share|improve this question




















  • 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















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:



Screen Shot










share|improve this question




















  • 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













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:



Screen Shot










share|improve this question















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:



Screen Shot







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












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 a default 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








share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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 a default 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








share|improve this answer





















  • 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















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 a default 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








share|improve this answer





















  • 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













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 a default 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








share|improve this answer












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 a default 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









share|improve this answer












share|improve this answer



share|improve this answer










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










  • 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


















  • 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen