-2

i've been trying to write this program and it just doesn't do what it is supposed to, and i've looked for solutions online but i can't figure out what's wrong, can you give me a hand? (The program is in Portuguese, because i was doing it in my language but it is quite understandable.

The exercise is: Write a program that allows you to indicate, from a determined number of hours, which minutes, seconds, or even the tenths of a second, that this number of hours contains using switch.

#include <stdio.h>
#include <stdlib.h>

int main()
{
 int h,m,s,ds;

printf("Introduza o nr de horas:\n");
scanf( "%d" , &h);

m = h*60;
s = h*60*60;
ds = h*60*60*10;

scanf( "%d%d%d" , &m, &s, &ds);

switch (h)
{
case 'a' : printf(" Tem: %d minutos" , m);
case 'b' : printf(" Tem: %d segundos" , s);
case 'c' : printf(" Tem: %d decimos de segundo" , ds);
default : printf("Medida incorreta");
}

}
  • 5
    Just guessing, but you should put `break;`s at the end of each `case`. But without knowing what' it's supposed to do and what it does instead it's hard to answer. – Federico klez Culloca Feb 04 '19 at 11:49
  • 1
    You're also ignoring the return value from `scanf()`, so you have no idea what you actually read in. – Andrew Henle Feb 04 '19 at 11:50
  • 1
    Anyway, welcome. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Federico klez Culloca Feb 04 '19 at 11:51
  • Please specify what exactly is wrong, inputs which should be passed to the program, expected outputs, and actual outputs. And at least start by enabling all warnings on your compiler (e.g. `-Wall -Wextra -Werror` on gcc), to get [rid of all suspicious constructs before you even run it](https://godbolt.org/z/mZm8vP). – vgru Feb 04 '19 at 11:52
  • 2
    And what is the point of your second `scanf()`? `scanf( "%d%d%d" , &m, &s, &ds);` can overwrite the values of `m`, `s`, and `ds` that you just set. – Andrew Henle Feb 04 '19 at 11:52
  • I think the problem should be: input any floating-point number of hours (ex: 6.11) and output the number of minutes, seconds, ... . This way I think we can't implement `switch` as it expects an `int` not `float`. – Gamal Othman Feb 04 '19 at 12:25

3 Answers3

1

I don't know what is the output you are getting and what you are expecting in the output, but by looking at your code, there is break statement after every case

for example

case 'a' : printf(" Tem: %d minutos" , m);
           break;

What break statement does that it stops the other cases to be executed

Vishwas Atrey
  • 286
  • 3
  • 9
1

You need more understanding of switch-case statements.
switch-case statements do not execute the code block after the case statement. It just jumps to the right statement.

Switch-Case Statement

After running the code block after the case statement, it just runs the code below - ignoring any other case statements.

So if you want to stop before the next case statement, you should use break, which will break the switch statement. (Get out of the switch statement)

As a result, your code would be like this.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int h,m,s,ds;

    printf("Introduza o nr de horas:\n");
    scanf( "%d" , &h);

    m = h*60;
    s = h*60*60;
    ds = h*60*60*10;

    scanf( "%d%d%d" , &m, &s, &ds);

    switch (h)
    {
    case 'a' : 
        {
            printf(" Tem: %d minutos" , m); 
            break;
        }
    case 'b' :
        { 
            printf(" Tem: %d segundos" , s);
            break;
        }
    case 'c' : 
        {
            printf(" Tem: %d decimos de segundo" , ds);
            break;
        }
    default : 
        {
            printf("Medida incorreta");
            break;
        }
    }

}

Since I thought you were learning about switch-case statements, I will not comment about the scanf's return value.

maxswjeon
  • 100
  • 1
  • 11
  • Use of brackets `{}` in cases in `switch` is not necessary and I think it make the code bulky. – Gamal Othman Feb 04 '19 at 12:46
  • @GamalOthman Use of brackets `{}` in switch-case statements enables variable declaration in the code block. Without it, we cannot declare variables. – maxswjeon Feb 04 '19 at 13:13
  • But here you are not declaring anything. If this is your style, your are free to keep it but I see that confusing. Also, answers on this question say that it is not recommended. – Gamal Othman Feb 04 '19 at 13:30
0

In provided exapmle, you first calculating values for m, s and ds from value of h, and then assigning (overwriting) new values within scanf().

Second - note, that you are using char literals in case statements, i.e. 'a', 'b', 'c', instead of integer, i.e. 1, 2, 3, despite that variable h stores integer.

First of all, it's a good idea to decide - which values must be in m, s and ds variables - calculated from h or entered by user.

Denis Golovkin
  • 106
  • 2
  • 11
  • `scanf("%d%d%d" , &var1, &var2, &var3)` will not produce any errors if inputs are separated by a whitespace character. – Gamal Othman Feb 04 '19 at 12:19