Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A program in C that takes a number from 1 to 10 as a user input
(unless sentinel value is provided), checks if the number is not odd, and if
not, multiply user’s input by 100, by using while, switch, if
statements and continue and brake keywords

example;
User input = 1
output shall be:
"Please enter an even number"
User input = 2
output shall be:
200


What I have tried:

C++
#include <stdio.h>

int main()
{
    int i, n;
    while(i<10) {
        printf("Enter a number");
        scanf("%d", &n);
        switch(i) {
            
          case 1;
            if(i%2=!0)
            printf("Enter an even number");
            break;
            case 2;
            i=i*100;
          break;
        
        }
    }
}
Posted
Updated 5-Oct-19 9:33am
v2

Your switch statement caters only for the numbers 1 or 2. But it should be switching on the type of number, i.e. odd or even. So try the following:
C++
int oddeven = i % 2;
switch (oddeven)
{
case 0:
    // number is even so do that processinhg
    break;

case 1:
    // number is odd
    break;
}
 
Share this answer
 
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Sorry to be this honest but... you don't have a lot of errors, your program is just wrong.

You are doing a loop of 10 iterations, where you ask for the number, then try to execute some things in 2 cases and forgetting the other 8.

I recommend you to re-read the theory of the "tools" you need to use because I think that you didn't understand it correctly.

Some considerations:
- Your loop will continue executing even if you give negative numbers (all are smaller than 10). What is the "sentinel value" you say?

- You are using "switch" directly with the input but only considering two cases, what happens with 3, 4, 5, and so on? In other words... I would not use the switch for that. Additionally you are missing a "default" path.


Some directions to correct it:
1- If you have to execute it until a sentinel value (whatever it is) is provided, then you have to ask first for the input, go into the loop if allowed while (input != sentinel_value), do what you have to do with the input and then ask for and get the next user input (the while will check it again and repeat if necessary)
2- I would check for 1 < input < 10 (as I don't know what your "sentinel value" means...) because you shouldn't execute anything if i.e. input = -123 (this could be a usage of "continue" to avoid the rest of the iteration and start the loop over again)
3- If you have to use switch mandatory, a good way to use it could be to separate even / odd numbers.
4- If you are not forced to use the switch, then the if / else with "input % 2" will do the job. (if odd, that could be another continue)
5- If you don't use the switch, you will need an usage for the "break" instruction, you could use it to break an endless loop (alternative to #1) if the sentinel value is given. I mean: while (1) { ask - get input, if (sentinel_value) break; else {do the rest} }
 
Share this answer
 
v3
Comments
Nelek 5-Oct-19 16:14pm    
You are welcome. Analyze what I told you and compare it with what you have done. Re-read the theory of the while / do{...}while() and switch and then try it again.
Don't forget that the debugger is a big help to find the things on your own (which will be the best master)
Nelek 5-Oct-19 16:21pm    
If you see it that negative... why are you learning to code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900