Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 名称不会出现在我的程序的底部_C++ - Fatal编程技术网

C++ 名称不会出现在我的程序的底部

C++ 名称不会出现在我的程序的底部,c++,C++,此程序用于我的作业,但程序底部没有显示输入的名称。为什么? 你们可以在Dev++中测试它,看看它是如何工作的 #include<stdio.h> int main(){ char n4m3[100],Pos; int rt,hr,gI,T,td,ss=100,pi=100,hc=100,NetIn; printf(" ==Employee Salary=="); printf("\n Name:"); scanf(" %s",&

此程序用于我的作业,但程序底部没有显示输入的名称。为什么? 你们可以在Dev++中测试它,看看它是如何工作的

#include<stdio.h>
int main(){

    char n4m3[100],Pos;
    int rt,hr,gI,T,td,ss=100,pi=100,hc=100,NetIn;

    printf("    ==Employee Salary==");
    printf("\n Name:");
    scanf(" %s",&n4m3);

    printf("\n -Position- \n  C-CEO\n  V-VP\n  S-Supervisor\n  T-Team Leader");
    printf("\n Postion:");
    scanf(" %s",&Pos);

    if (Pos=='C'||Pos=='c')
        {
            rt=500;
            printf("\n  CEO Rate:500");
        }
        else if (Pos=='V'||Pos=='v')
        {
            rt=400;
            printf("\n  VP Rate:400");
        }
        else if (Pos=='S'||Pos=='s')
        {
            rt=300;
            printf("\n  Supervisor Rate:300");
        }
        else if (Pos=='T'||Pos=='t')
        {
            rt=200;
            printf("\n  Team Leader Rate:200");
        }
    else printf("   Invalid Input");

    printf("\n  Number of Hours Worked:");
    scanf("%d",&hr);

    printf("\n  ==Summary==");
    gI=rt*hr;
    printf("\n Gross Income:%d",gI);

    if (gI>=4000)

        T=gI*.4;
    else if (gI>=3000)
        T=gI*.3;
    else if (gI>=2000)
        T=gI*.2;
    else if (gI>=1000)
        T=gI*.1;

        printf("\n Tax:%d",T);
        td=T+ss+pi+hc;
        printf("\n Total Deductions:%d",td);
        NetIn=gI-td;
        printf("\n Net Income is %d",NetIn);
        printf("\n----------------------");
        printf("\n Mr./Ms. %s your net income is %d",n4m3,NetIn);
}
#包括
int main(){
字符n4m3[100],位置;
int-rt,hr,gI,T,td,ss=100,pi=100,hc=100,NetIn;
printf(“==员工工资==”);
printf(“\n名称:”);
扫描频率(“%s”和n4m3);
printf(“\n-职位-\n C-CEO\n V-VP\n S-Supervisor\n T-Team Leader”);
printf(“\n位置:”);
scanf(“%s”和“&Pos”);
如果(Pos=='C'| | Pos=='C')
{
rt=500;
printf(“\n首席执行官费率:500”);
}
else if(Pos='V'| | Pos='V')
{
rt=400;
printf(“\n VP速率:400”);
}
else if(Pos=='S'| | Pos=='S')
{
rt=300;
printf(“\n主管费率:300”);
}
else if(Pos='T'| | Pos=='T')
{
rt=200;
printf(“\n组长费率:200”);
}
else printf(“无效输入”);
printf(“\n工作小时数:”);
扫描频率(“%d”、&hr);
printf(“\n==摘要==”);
gI=rt*hr;
printf(“\n总收入:%d”,gI);
如果(gI>=4000)
T=gI*.4;
否则,如果(gI>=3000)
T=gI*.3;
否则,如果(gI>=2000)
T=gI*.2;
否则,如果(gI>=1000)
T=gI*.1;
printf(“\n税款:%d”,T);
td=T+ss+pi+hc;
printf(“\n扣减总额:%d”,td);
NetIn=gI-td;
printf(“\n净收入为%d”,NetIn);
printf(“\n-------------------”);
printf(“\n先生/女士%s您的净收入是%d”,n4m3,NetIn);
}

这一部分应该显示用户的姓名和净收入,但它不显示姓名

按预期工作。程序结束后,带有结果的命令行窗口将关闭,因此您无法看到它。

按预期工作。程序刚结束时,带有结果的命令行窗口就关闭了,所以您看不到它。

当您输入一个没有空格的字符串时,它会正常工作。 但是,如果要输入一个带有空格的字符串,它的行为会发生变化,因为它将只读取到空格处的字符串

为此,您可以在代码中使用getline、fgets或sscanf而不是scanf

尝试替换该行:

scanf(" %s",&n4m3);

fgets(n4m3100标准)


它肯定会工作。

当您输入一个没有空格的字符串时,它会工作正常。 但是,如果要输入一个带有空格的字符串,它的行为会发生变化,因为它将只读取到空格处的字符串

为此,您可以在代码中使用getline、fgets或sscanf而不是scanf

尝试替换该行:

scanf(" %s",&n4m3);

fgets(n4m3100标准)


它肯定会工作。

它不工作,因为
scanf
需要一个
char*
,而您正在给它传递一个
char(*)[100]
。要解决这个问题,请记住数组是一个连续的内存块,数组的名称是指向该内存开头的指针。因此,
n4m3
已经是一个
char*
,不需要用
来获取它的地址

该行应为
scanf(“%s”,n4m3)//没有“&”

相关注释 我通过编译启用了警告的代码发现了这一点。你应该总是这样做!在Linux或Mac上使用铿锵标志编译
-Wall-Wextra-pedantic
,会为此代码产生多个警告。有关的建议是:

tmp.cpp:9:13: error: format specifies type 'char *' but the argument has type
  'char (*)[100]' [-Werror,-Wformat]
scanf(" %s",&n4m3);
        ~~  ^~~~~
谷歌搜索错误给我带来了麻烦,解决了问题。另一个警告是:

tmp.cpp:51:10: error: variable 'T' is used uninitialized whenever 'if' condition
      is false [-Werror,-Wsometimes-uninitialized]
else if (gI>=1000)
         ^~~~~~~~
tmp.cpp:54:24: note: uninitialized use occurs here
    printf("\n Tax:%d",T);
                       ^
tmp.cpp:51:6: note: remove the 'if' if its condition is always true
else if (gI>=1000)
     ^~~~~~~~~~~~~
tmp.cpp:5:15: note: initialize the variable 'T' to silence this warning
int rt,hr,gI,T,td,ss=100,pi=100,hc=100,NetIn;
              ^
               = 0

有更多的警告,我会让你的编译器(一旦你设置正确)发现。在Windows上,通过使用增加Visual Studio的可用性。如果让编译器为您分析代码,您将节省大量的工作时间。

它不起作用,因为
scanf
需要
char*
而您正在向它传递
char(*)[100]
。要解决这个问题,请记住数组是一个连续的内存块,数组的名称是指向该内存开头的指针。因此,
n4m3
已经是一个
char*
,不需要用
来获取它的地址

该行应为
scanf(“%s”,n4m3)//没有“&”

相关注释 我通过编译启用了警告的代码发现了这一点。你应该总是这样做!在Linux或Mac上使用铿锵标志编译
-Wall-Wextra-pedantic
,会为此代码产生多个警告。有关的建议是:

tmp.cpp:9:13: error: format specifies type 'char *' but the argument has type
  'char (*)[100]' [-Werror,-Wformat]
scanf(" %s",&n4m3);
        ~~  ^~~~~
谷歌搜索错误给我带来了麻烦,解决了问题。另一个警告是:

tmp.cpp:51:10: error: variable 'T' is used uninitialized whenever 'if' condition
      is false [-Werror,-Wsometimes-uninitialized]
else if (gI>=1000)
         ^~~~~~~~
tmp.cpp:54:24: note: uninitialized use occurs here
    printf("\n Tax:%d",T);
                       ^
tmp.cpp:51:6: note: remove the 'if' if its condition is always true
else if (gI>=1000)
     ^~~~~~~~~~~~~
tmp.cpp:5:15: note: initialize the variable 'T' to silence this warning
int rt,hr,gI,T,td,ss=100,pi=100,hc=100,NetIn;
              ^
               = 0

有更多的警告,我会让你的编译器(一旦你设置正确)发现。在Windows上,通过使用增加Visual Studio的可用性。如果让编译器为您分析代码,您将节省大量的工作时间。

欢迎使用堆栈溢出。简化这不仅仅是为了我们的方便,也是为了发展您的编码技能——这是一项至关重要的能力。尽量简化此代码,同时仍会产生相同的错误;错误将出现在剩下的代码中,就像一条鱼在排水池塘的最后一个水坑中一样。建议:尝试更改
scanf(“%s”、&n4m3)
scanf(“%s”,n4m3)@paulsm4它仍然是相同的,没有显示任何名称。请访问堆栈溢出。简化这不仅仅是为了我们的方便,也是为了发展您的编码技能——这是一项至关重要的能力。尽量简化此代码,同时仍会产生相同的错误;错误将出现在剩下的代码中,就像一条鱼在排水池塘的最后一个水坑中一样。建议:尝试更改
scanf(“%s”、&n4m3)
scanf(“%s”,n4m3)@paulsm4它仍然是一样的没有名字是shownum。。。不。问题比那更严重。也许吧,但他没有提供任何测试数据,这是一个很常见的错误,所以我不得不提一下。嗯。。。不,问题更严重