【C语言求助】和其他代码并列执行此代码时输出空心方框

飞禽走兽卍一2b 2021-03-05 11:31:30
(本代码目的是将输入的字符换成后面第四个字母,China——>Glmre) 问题描述: 当独立执行此段代码时输出:Glmre 当和其他代码并列执行时输出:口Glmr #include <math.h> #include <stdio.h> int main(void) { printf("hw 3.6\n"); printf("请输入五位数密码:\n"); char c[5]; int i; for(i=0;i<6;i++) //for循环输入多个字符 { c[i]=getchar(); //将输入的字符赋给变量 } for(i=0;i<6;i++) { c[i]=c[i]+4; //将输入的字符换成后面第四个 c[5]='\0'; //结束 putchar(c[i]); } putchar('\n'); } 请大家帮忙看看是哪的问题,谢谢!
...全文
470 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-03
  • 打赏
  • 举报
回复
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html
qzjhjxj 2021-03-06
  • 打赏
  • 举报
回复
供参考:
//问题描述:
//当独立执行此段代码时输出:Glmre
//当和其他代码并列执行时输出:口Glmr

#include <math.h>
#include <stdio.h>
int main(void)
{
     printf("hw 3.6\n");
     printf("请输入五位数密码:\n");
     char c[5];       //这里c[5],下标为c[0]---c[4]
     int i;
     for(i=0;i<5;i++)       //for(i=0;i<6;i++)   
      {
        c[i]=getchar();    
      }
     for(i=0;i<5;i++)     //for(i=0;i<6;i++)
      {
        c[i]=c[i]+4;    
                           //c[5]='\0'; //结束 总共5位的数组,c[5]下标越界了,没有放'\0'结束符的位置了,这是字符数组,
                                      //没必要有结束标志。
        putchar(c[i]);
      }
     putchar('\n');
     
     return 0;
}

//hw 3.6
//请输入五位数密码:
//China                  (5位)
//Glmre                   (5位)
//请按任意键继续. . .
qzjhjxj 2021-03-06
  • 打赏
  • 举报
回复
代码里有输入金额、存款方式等,象我们敲完3.4,接下去就敲一个回车,这个回车其实也被读入的,这就是多余的字符。你可以百度下scanf()和getchar()等几个函数的详细用法,了解下输入输出时的一些细节的东西。
  • 打赏
  • 举报
回复
引用 9 楼 qzjhjxj的回复:
供参考:
#include <math.h>
#include <stdio.h>
int main(void)
{
 //以下为并列的代码,不是主要问题
   {
          printf("hw 3.2\n");
          float way,temp;
          printf("请输入金额:\n");
          scanf("%f",&temp);
          printf("请输入存款方式(1-5):\n");
          scanf("%f",&way);
          if(way==1)
          {
                temp*=(1+5*0.03);
                printf("result= %f \n\n",temp);
          }
          else if(way==2)
         {
              temp*=(1+2*0.021);
              temp*=(1+3*0.0275);
              printf("result= %f \n\n",temp);
         }
         else if(way==3)
        {
              temp*=(1+3*0.0275);
              temp*=(1+2*0.021);
              printf("result= %f \n\n",temp);
        }  
        else if(way==4)
        {   
              temp*=pow(1.015,5); 
              printf("result= %f \n\n",temp);
        } 
        else
        {
              temp*=pow(1+0.0035/4,20);                
              printf("result=%f \n\n",temp); 
        }  
    }

    getchar();//这里加一句,吸收前面输入过程中留在缓冲区多余的字符。

//以下为提问的代码
    {
         printf("hw 3.6\n");       
         printf("请输入五位数密码:\n");       
         char c[5];
         int i;       
         for(i=0;i<5;i++)      //for循环输入多个字符   
    {         
         c[i]=getchar();      //将输入的字符赋给变量
    }     
         for(i=0;i<5;i++)    
    {           
         c[i]=c[i]+4;         //将输入的字符换成后面第四个                    
                              //c[5]='\0';
         putchar(c[i]);
    }       
         putchar('\n');
    }
    system("pause");
    return 0;
}



//hw 3.2
//请输入金额:
//3.4
//请输入存款方式(1-5):
//5
//result=3.459997

//hw 3.6
//请输入五位数密码:
//China
//Glmre
//请按任意键继续. . .
哇⊙∀⊙!太棒了! 请问这里“留在前面缓冲区的字符”是什么?从哪里来的? 谢谢!
qzjhjxj 2021-03-06
  • 打赏
  • 举报
回复
供参考:
#include <math.h>
#include <stdio.h>
int main(void)
{
 //以下为并列的代码,不是主要问题
   {
          printf("hw 3.2\n");
          float way,temp;
          printf("请输入金额:\n");
          scanf("%f",&temp);
          printf("请输入存款方式(1-5):\n");
          scanf("%f",&way);
          if(way==1)
          {
                temp*=(1+5*0.03);
                printf("result= %f \n\n",temp);
          }
          else if(way==2)
         {
              temp*=(1+2*0.021);
              temp*=(1+3*0.0275);
              printf("result= %f \n\n",temp);
         }
         else if(way==3)
        {
              temp*=(1+3*0.0275);
              temp*=(1+2*0.021);
              printf("result= %f \n\n",temp);
        }  
        else if(way==4)
        {   
              temp*=pow(1.015,5); 
              printf("result= %f \n\n",temp);
        } 
        else
        {
              temp*=pow(1+0.0035/4,20);                
              printf("result=%f \n\n",temp); 
        }  
    }

    getchar();//这里加一句,吸收前面输入过程中留在缓冲区多余的字符。

//以下为提问的代码
    {
         printf("hw 3.6\n");       
         printf("请输入五位数密码:\n");       
         char c[5];
         int i;       
         for(i=0;i<5;i++)      //for循环输入多个字符   
    {         
         c[i]=getchar();      //将输入的字符赋给变量
    }     
         for(i=0;i<5;i++)    
    {           
         c[i]=c[i]+4;         //将输入的字符换成后面第四个                    
                              //c[5]='\0';
         putchar(c[i]);
    }       
         putchar('\n');
    }
    system("pause");
    return 0;
}



//hw 3.2
//请输入金额:
//3.4
//请输入存款方式(1-5):
//5
//result=3.459997

//hw 3.6
//请输入五位数密码:
//China
//Glmre
//请按任意键继续. . .
  • 打赏
  • 举报
回复
引用 7 楼 qzjhjxj的回复:
错误用红字标出:
引用
飞禽走兽卍一2b 等级 #include <math.h> #include <stdio.h> int main(void) { //以下为并列的代码,不是主要问题 { printf("hw 3.2\n"); float way,temp; printf("请输入金额:\n"); scanf("%f",&temp); printf("请输入存款方式(1-5):\n"); scanf("%f",&way); if(way==1) { temp*=(1+5*0.03); printf("result= %f \n\n",temp); } else if(way==2) { temp*=(1+2*0.021); temp*=(1+3*0.0275); printf("result= %f \n\n",temp); } else if(way==3) { temp*=(1+3*0.0275); temp*=(1+2*0.021); printf("result= %f \n\n",temp); } else if(way==4) { temp*=pow(1.015,5); printf("result= %f \n\n",temp); } else { temp*=pow(1+0.0035/4,20); printf("result=%f \n\n",temp); } } //以下为提问的代码 { printf("hw 3.6\n"); printf("请输入五位数密码:\n"); char c[5]; int i; for(i=0;i<5;i++) //for循环输入多个字符 { c[i]=getchar(); //将输入的字符赋给变量 } for(i=0;i<5;i++) { c[i]=c[i]+4; //将输入的字符换成后面第四个 c[5]='\0'; //这里多了对c[5]数组的操作,而且下标为5,下标越界。 putchar(c[i]); } putchar('\n'); } }
还是不行。。。X﹏X 输出结果是 口Glmr
qzjhjxj 2021-03-06
  • 打赏
  • 举报
回复
错误用红字标出:
引用
飞禽走兽卍一2b 等级 #include <math.h> #include <stdio.h> int main(void) { //以下为并列的代码,不是主要问题 { printf("hw 3.2\n"); float way,temp; printf("请输入金额:\n"); scanf("%f",&temp); printf("请输入存款方式(1-5):\n"); scanf("%f",&way); if(way==1) { temp*=(1+5*0.03); printf("result= %f \n\n",temp); } else if(way==2) { temp*=(1+2*0.021); temp*=(1+3*0.0275); printf("result= %f \n\n",temp); } else if(way==3) { temp*=(1+3*0.0275); temp*=(1+2*0.021); printf("result= %f \n\n",temp); } else if(way==4) { temp*=pow(1.015,5); printf("result= %f \n\n",temp); } else { temp*=pow(1+0.0035/4,20); printf("result=%f \n\n",temp); } } //以下为提问的代码 { printf("hw 3.6\n"); printf("请输入五位数密码:\n"); char c[5]; int i; for(i=0;i<5;i++) //for循环输入多个字符 { c[i]=getchar(); //将输入的字符赋给变量 } for(i=0;i<5;i++) { c[i]=c[i]+4; //将输入的字符换成后面第四个 c[5]='\0'; //这里多了对c[5]数组的操作,而且下标为5,下标越界。 putchar(c[i]); } putchar('\n'); } }
  • 打赏
  • 举报
回复
引用 4 楼 qzjhjxj的回复:
当和其他代码并列执行?把代码补全贴上来看看。这里对c[5]数组的操作,下标0-4,或者你不能按字符串的形式输出。
放在评论区里了
  • 打赏
  • 举报
回复
#include <math.h> #include <stdio.h> int main(void) { //以下为并列的代码,不是主要问题 { printf("hw 3.2\n"); float way,temp; printf("请输入金额:\n"); scanf("%f",&temp); printf("请输入存款方式(1-5):\n"); scanf("%f",&way); if(way==1) { temp*=(1+5*0.03); printf("result= %f \n\n",temp); } else if(way==2) { temp*=(1+2*0.021); temp*=(1+3*0.0275); printf("result= %f \n\n",temp); } else if(way==3) { temp*=(1+3*0.0275); temp*=(1+2*0.021); printf("result= %f \n\n",temp); } else if(way==4) { temp*=pow(1.015,5); printf("result= %f \n\n",temp); } else { temp*=pow(1+0.0035/4,20); printf("result=%f \n\n",temp); } } //以下为提问的代码 { printf("hw 3.6\n"); printf("请输入五位数密码:\n"); char c[5]; int i; for(i=0;i<5;i++) //for循环输入多个字符 { c[i]=getchar(); //将输入的字符赋给变量 } for(i=0;i<5;i++) { c[i]=c[i]+4; //将输入的字符换成后面第四个 c[5]='\0'; putchar(c[i]); } putchar('\n'); } }
qzjhjxj 2021-03-06
  • 打赏
  • 举报
回复
当和其他代码并列执行?把代码补全贴上来看看。这里对c[5]数组的操作,下标0-4,或者你不能按字符串的形式输出。
  • 打赏
  • 举报
回复
引用 1 楼 qzjhjxj的回复:
供参考:
//问题描述:
//当独立执行此段代码时输出:Glmre
//当和其他代码并列执行时输出:口Glmr

#include <math.h>
#include <stdio.h>
int main(void)
{
     printf("hw 3.6\n");
     printf("请输入五位数密码:\n");
     char c[5];       //这里c[5],下标为c[0]---c[4]
     int i;
     for(i=0;i<5;i++)       //for(i=0;i<6;i++)   
      {
        c[i]=getchar();    
      }
     for(i=0;i<5;i++)     //for(i=0;i<6;i++)
      {
        c[i]=c[i]+4;    
                           //c[5]='\0'; //结束 总共5位的数组,c[5]下标越界了,没有放'\0'结束符的位置了,这是字符数组,
                                      //没必要有结束标志。
        putchar(c[i]);
      }
     putchar('\n');
     
     return 0;
}

//hw 3.6
//请输入五位数密码:
//China                  (5位)
//Glmre                   (5位)
//请按任意键继续. . .
还是输出了口Glmre
  • 打赏
  • 举报
回复
引用 1 楼 qzjhjxj的回复:
供参考:
//问题描述:
//当独立执行此段代码时输出:Glmre
//当和其他代码并列执行时输出:口Glmr

#include <math.h>
#include <stdio.h>
int main(void)
{
     printf("hw 3.6\n");
     printf("请输入五位数密码:\n");
     char c[5];       //这里c[5],下标为c[0]---c[4]
     int i;
     for(i=0;i<5;i++)       //for(i=0;i<6;i++)   
      {
        c[i]=getchar();    
      }
     for(i=0;i<5;i++)     //for(i=0;i<6;i++)
      {
        c[i]=c[i]+4;    
                           //c[5]='\0'; //结束 总共5位的数组,c[5]下标越界了,没有放'\0'结束符的位置了,这是字符数组,
                                      //没必要有结束标志。
        putchar(c[i]);
      }
     putchar('\n');
     
     return 0;
}

//hw 3.6
//请输入五位数密码:
//China                  (5位)
//Glmre                   (5位)
//请按任意键继续. . .
还是不行啊。。。
  • 打赏
  • 举报
回复
引用 11 楼 qzjhjxj的回复:
代码里有输入金额、存款方式等,象我们敲完3.4,接下去就敲一个回车,这个回车其实也被读入的,这就是多余的字符。你可以百度下scanf()和getchar()等几个函数的详细用法,了解下输入输出时的一些细节的东西。
好的,谢谢! (晚上早点睡哦)

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧