Jump to content

Hello there, I was tasked with making a console program that calculates taxes then ask the user if he wants to restart or leave by pressing 1 or 0 but the program only reads the false condition (leave). I tried to find the error even asking a coder friend but he doesnt do C anymore so he didnt find it. The program is meant to be built around loops so no advanced solutions will fix it. Excuse the french commentary, thx. 

 

 

 

program3.PNG

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/
Share on other sites

Link to post
Share on other sites

Have you tried converting the user input from string to integer? I doubt it's the error but worth a shot. I haven't done strict C so I am not sure if scanf will set the input to be the value stored at the second arguement. If this code is in C, not just C++ then I'd do 

 

"cin >> ichoix1"

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9346309
Share on other sites

Link to post
Share on other sites

First, ALWAYS initiate your flags, preferably all variables. It should read 

int iChoix1 = 1;

 

 

Second, you should be checking if iChoix1 doesn't equal 0, not if it equals 1. Enter 

while (!iChoix1)

 

 

Third, just to track this kind of stuff you should have a debug define. At the start of the code after the includes, add

#DEFINE ENABLE_DEBUG

Then, after the scanf("%i", iChoix1) line, add

#ifdef ENABLE_DEBUG

printf("The user entered %i", iChoix1);

#endif[code]

 

You can do the same thing (but with a different printf if you want) anywhere else. If you comment out the

[code]#DEFINE ENABLE_DEBUG

line all that code will effectively be removed when you compile. It's a way to add or remove all your debug code from the final product with a single action. 

 

Edit: I messed up some of the code tags and I can't fix it from my phone. Would one of the moderators be so kind as to help? The one after the #endif needs to be a closing tag. @wkdpaul

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9346413
Share on other sites

Link to post
Share on other sites

2 minutes ago, Xaer0 said:

When you are scanning the 0 or 1 for whether to quit or not, you are using '%i', I think you want to use '%d' for integers.

That's for decimals. He's right to use %i for (signed) integers. 

http://www.cplusplus.com/reference/cstdio/scanf/

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9346671
Share on other sites

Link to post
Share on other sites

20 minutes ago, JoostinOnline said:

That's for decimals. He's right to use %i for (signed) integers. 

http://www.cplusplus.com/reference/cstdio/scanf/

%i will allow any kind of integer, whether it be decimal, Octal, and Hexadecimal. Where as %d will only permit decimal integers (0-9). But Looking at it again, I don't think that is the problem anyway :o

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9346737
Share on other sites

Link to post
Share on other sites

10 hours ago, Xaer0 said:

%i will allow any kind of integer, whether it be decimal, Octal, and Hexadecimal. Where as %d will only permit decimal integers (0-9). But Looking at it again, I don't think that is the problem anyway :o

No, %i will not allow for decimals. It will convert it to a whole number. 

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9348903
Share on other sites

Link to post
Share on other sites

13 minutes ago, fizzlesticks said:

Decimal is referring to base 10, not a number with a decimal point.

Oh, I see what he's saying. We were just discussing %d which is for decimals (with a decimal point), thus my confusion. 

 

Anyway, using %i will cast the value as a signed 32 bit integer. 

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9349062
Share on other sites

Link to post
Share on other sites

7 minutes ago, JoostinOnline said:

We were just discussing %d which is for decimals (with a decimal point), thus my confusion. 

No, %d is for integers but only accepts base 10 unlike %i that accepts base 8, 10 or 16. %f, %e, %g and %a are for floating point numbers.

 

edit: This page has a chart with all the format specifiers http://www.cplusplus.com/reference/cstdio/scanf/

1474412270.2748842

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9349093
Share on other sites

Link to post
Share on other sites

Accepting and displaying are two different things. 

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9349148
Share on other sites

Link to post
Share on other sites

Thanks for the replies, i do 12 hours shift on weekends so il check monday and reply to everyone. Il make sure to copy the code next time too!

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9354202
Share on other sites

Link to post
Share on other sites

On 11/02/2017 at 2:04 AM, JoostinOnline said:

First, ALWAYS initiate your flags, preferably all variables. It should read 


int iChoix1 = 1;

 

 

Second, you should be checking if iChoix1 doesn't equal 0, not if it equals 1. Enter 


while (!iChoix1)

 

 

Third, just to track this kind of stuff you should have a debug define. At the start of the code after the includes, add


#DEFINE ENABLE_DEBUG

Then, after the scanf("%i", iChoix1) line, add


#ifdef ENABLE_DEBUG

printf("The user entered %i", iChoix1);

#endif[code]

 

You can do the same thing (but with a different printf if you want) anywhere else. If you comment out the

[code]#DEFINE ENABLE_DEBUG

line all that code will effectively be removed when you compile. It's a way to add or remove all your debug code from the final product with a single action. 

 

Edit: I messed up some of the code tags and I can't fix it from my phone. Would one of the moderators be so kind as to help? The one after the #endif needs to be a closing tag. @wkdpaul

The problem is still there.

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360646
Share on other sites

Link to post
Share on other sites

On 11/02/2017 at 7:36 AM, NoGravityPanda said:

I think your problem is in your while condition, try instead of doing 


iChoix1==1

just do


while(iChoix1)

and just saying- you should never have "junk" in your variables. 


int iChoix1=1;

etc.

The problem is still there

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360648
Share on other sites

Link to post
Share on other sites

8 minutes ago, M.Yurizaki said:

I typed in your program pretty much word for word, except for system("pause") and used scanf("%d", input); instead of %i. I got it to work on my end.

can you be kind enough to send me your code so I can run it and verify if the problem is software or setting sided

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360763
Share on other sites

Link to post
Share on other sites

3 minutes ago, TheRedViper said:

can you be kind enough to send me your code so I can run it and verify if the problem is software or setting sided

I've taken the liberty to do some "optimizations" while I was at it.

#import <stdio.h>
#import <stdlib.h>

int main(){
  float money, tps, tvq;
  int input = 0;
  
  do {
    printf("Enter the amount of moeny: ");
    scanf("%f", &money);
    printf("Money before taxes: %.2fn\n", money);
    
    tps = money * 5 / 100;
    printf("TPS: %.2f\n", tps);
    
    tvq = (tps + money) * 9.975 / 100;
    printf("TVQ: %.2f\n", tvq);
    printf("Money total: %.2f\n", tvq + money);
    printf("Enter 1 to do again, 0 to quit: ");
    scanf("%d", &input);
    
  } while(input == 1);
}

 

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360802
Share on other sites

Link to post
Share on other sites

8 minutes ago, M.Yurizaki said:

I've taken the liberty to do some "optimizations" while I was at it.


#import <stdio.h>
#import <stdlib.h>

int main(){
  float money, tps, tvq;
  int input = 0;
  
  do {
    printf("Enter the amount of moeny: ");
    scanf("%f", &money);
    printf("Money before taxes: %.2fn\n", money);
    
    tps = money * 5 / 100;
    printf("TPS: %.2f\n", tps);
    
    tvq = (tps + money) * 9.975 / 100;
    printf("TVQ: %.2f\n", tvq);
    printf("Money total: %.2f\n", tvq + money);
    printf("Enter 1 to do again, 0 to quit: ");
    scanf("%d", &input);
    
  } while(input == 1);
}

 

Doesn't work on my end. Il ask my teacher tomorrow and come back to you, thx for helping.

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360826
Share on other sites

Link to post
Share on other sites

4 minutes ago, TheRedViper said:

Doesn't work on my end. Il ask my teacher tomorrow and come back to you, thx for helping.

Please describe what you mean by "doesn't work."

 

I've compiled this with gcc and it seems to work just fine.

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360834
Share on other sites

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

Please describe what you mean by "doesn't work."

 

I've compiled this with gcc and it seems to work just fine.

it doesnt restart just asks again enter 1 or 0 until i enter anything else than 1

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360839
Share on other sites

Link to post
Share on other sites

6 minutes ago, TheRedViper said:

it doesnt restart just asks again enter 1 or 0 until i enter anything else than 1

So the output is basically:

Money before taxes: (something)
TPS: (something)
TVQ = (something)
Money total: (something)

Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
[repeat forever]

 

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360875
Share on other sites

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

So the output is basically:


Money before taxes: (something)
TPS: (something)
TVQ = (something)
Money total: (something)

Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
Enter 1 to do again, 0 to quit: 1
[repeat forever]

 

yeah

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/736265-c-coding-help/#findComment-9360878
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×