Suat Hyusein
Suat Hyusein

Reputation: 595

Having problems with switch statements

I've been trying to get this code working, but somehow I can't do it..

#include <iostream.h>
#include <stdio.h>

int main() {

    int a,b,c;
    int y=3;
    int i=2;
    int g[] = {20};
    int m,k;
    int Z;

    printf("Enter a number for a");
    scanf("%d", &a);

    printf("Enter a number for b");
    scanf("%d", &b);

    printf("Enter a number for c");
    scanf("%d", &c);

    m=y;


    do
    {

        Z = a+b-c;

        switch(Z)
        {
        case '0':
        case '1': 
                k=17;
                m+=b;
                break;
        case '2': 
                m+=b;
                m=a;
                break;
        case '3':
                m=a-c;
                m+=b;
                m=a;
                break;
        case '7':
                m+=b;
                break;
        default:
                m=a;
                break;
        }

        g[m] = m%i;
        m--;



    }while(m>b);



}

This is the scheme that I had to turn into coding. http://ff.tu-sofia.bg/PIK/Izpiti/MidTest07.html

y and i are 3 and 2 by default, the array g should contain 20 integers, and the users have to type values for a, b and c.

Upvotes: 0

Views: 1061

Answers (1)

jramirez
jramirez

Reputation: 8685

Z is an integer and your cases are looking for strings. get rid of the quotes around the numbers in the cases.

The array g does not contain 20 integers it contains one element 20 in this case. I think what you meant was g[20] = {}

Also How is Z calculated ? Is it (a+b)-c or a+(b-c)? You need parentheses to make your intent clearer

Z = a+b-c;

 switch(Z)
    {
    case 0:
    case 1: 
            k=17;
            m+=b;
            break;
    case 2: 
            m+=b;
            m=a;
            break;

Upvotes: 3

Related Questions