2015-09-21 2 views
-7
#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 недель, но это не сработало.Почему эта программа не работает? Может ли кто-нибудь сказать мне, что делать?

+7

Я предлагаю вам выбрать хорошую книгу [здесь] (http://stackoverflow.com/questions/388242/the-definitive- c-book-guide-and-list) и изучите основы. – vsoftco

+2

Добро пожаловать в переполнение стека! Пожалуйста, прочитайте руководство [Как задать хороший вопрос] (http://stackoverflow.com/help/how-to-ask), особенно часть на примере Minimal, Complete и Verifiable (MCVE). Это поможет вам решить проблемы для себя. Если вы сделаете это и все еще застряли, вы можете вернуться и опубликовать свой MCVE, что вы пробовали, и каковы результаты, чтобы мы могли лучше вам помочь. – JeffC

+0

Google перевод с польского: Podaj liczbe -> Введите номер, nLiczba -> nNumber, 1 singn number -> 1 singn number, 2 поет номер -> 2 поет номер – MikeCAT

ответ

0

Не зная о поведении, этот код может быть скомпилирован.

#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() ; 
} 
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() ; 
} 
Смежные вопросы