C++ 为什么这个程序不起作用?谁能告诉我该怎么办?
我正在做一个简单的程序,我不知道为什么我会有这么多错误。有人能帮我吗?程序应该要求用户给他一个数字,然后将其与四组中的一组匹配。我尝试了两周,但没有成功。由于不知道该行为,此代码可以成功编译C++ 为什么这个程序不起作用?谁能告诉我该怎么办?,c++,if-statement,switch-statement,C++,If Statement,Switch Statement,我正在做一个简单的程序,我不知道为什么我会有这么多错误。有人能帮我吗?程序应该要求用户给他一个数字,然后将其与四组中的一组匹配。我尝试了两周,但没有成功。由于不知道该行为,此代码可以成功编译 #include <iostream> #include <conio.h> int main () { int nNumber ; std :: cout << "Type a number: "; std :: cin >> nNumber ; in
#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 () ;
}
下面是更正后的代码,解释为注释
#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 () ;
}
我建议你从中挑选一本好书并学习基础知识。欢迎来到Stack Overflow!请阅读指南,特别是关于最小、完整和可验证示例MCVE的部分。这将帮助你自己解决问题。如果你这样做了,但仍然卡住了,你可以回来发布你的MCVE,你尝试了什么,结果是什么,这样我们可以更好地帮助你。谷歌翻译波兰语:Podaj liczbe->输入数字,nLiczba->nNumber,1个数字->1个数字,2个数字->2个数字什么是1个数字和2个数字?
#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 () ;
}