为什么这个程序不起作用?谁能告诉我该怎么办?

时间:2015-09-21 04:08:37

标签: c++ if-statement switch-statement

#include <iostream>
#include <conio.h>

int main () 
{ 
int nNumber ;
std :: cout << "Type a number: ";
std :: cin >> nNumber ;
int a = 0.0 ;
const int b = 1-9 ;
const int c = 
{ 
if (nNumber <= 10)
{
    nNumber = c ;
}nNumber    
 } 
const int d ;
{
if (nNumber > 0 )
{
    nNumber = d ;
}
}
switch (nNumber) ;
{
case a : std :: cout << "else" ;
case b : std :: cout << "1 singn number" ;
case c : std :: cout << "2 sings number" ;
case d : std :: cout << "negative number" ;
}
getch () ;
}

我制作了一个简单的程序,但我不知道为什么会有这么多错误。谁能帮我?该程序应该要求用户给他一个号码,并在那场比赛后给四个组中的一个。我试着这样做了两个星期,但它没有用。

2 个答案:

答案 0 :(得分:0)

以下是更正后的代码,并附有说明

#include <iostream>
#include <conio.h>

int main () 
{ 
    int nNumber ;
    std :: cout << "Type a number: ";
    std :: cin >> nNumber ;
    int a = 0 ; // a is an integer so it can store integer values only
    const int b = 1-9 ;
      int c; // c cannot be constant since it is changing its value, constans cannot change value during execution
    if (nNumber <= 10) // no need of {} if only one code is there to execute and befre the if, They are used to separate a block of code
        nNumber = c ;
// }nNumber this is wrong , no use with this code, and u have put ; in statement end
      int d ;// d cannot be constant since it is changing its value, constans cannot change value during execution
    if (nNumber > 0 )
    { // this is not neccessary since only one line of code is there to execute, if more than one it is neccessary, I put this here only to show thiy way is also not wrong
        nNumber = d ;
    } // every { shpould be closed 

    switch(nNumber)  // switch should not cotain ; at this point, refer the syntax of switch case, every case should contain braek statement, otherwise it will not break after the exection of case, ie if case 2 executing it will continue the execution until a break found or end of the switch,(in switch case)
    {
    case 1: 
        std :: cout << "else" ;
        break;
    case 2 : std :: cout << "1 singn number" ;
        break;
    case 3 : std :: cout << "2 sings number" ;
        break;
    case 4 : std :: cout << "negative number" ;
        break;
    }
    getch () ;
}

答案 1 :(得分:-1)

不知道行为,可以成功编译此代码。

#include <iostream>
// not needed and generates error on some compilers
//#include <conio.h>
int main () 
{ 
    int nLiczba ;
    std :: cout << "Podaj liczbe" ;
    std :: cin >> nLiczba ;
    const int a = 0 ; // You shouldn't give real number to int, and make this const
    const int b = 1-9 ;
    const int c = 12345; // I don't know the right value
    { 
        if (nLiczba <= 10)
        {
            nLiczba = c ;
        }

    } 
    const int d = 67890; // I don't know the right value
    {
        if (nLiczba > 0 )
        {
            nLiczba = d ;
        }
    }
    switch (nLiczba) // remove junk semicolon
    {
        // add break;s
        case a : std :: cout << "else" ; break;
        case b : std :: cout << "1 singn number" ; break;
        case c : std :: cout << "2 sings number" ; break;
        case d : std :: cout << "negative number" ; break;
    }
    // not needed and generates error on some compilers
    //getch () ;
}