简体   繁体   English

程序帮助! 用户点击进入程序后,程序结束

[英]Program help! After user hits enter the program ends

Ok I am making a program that reads two characters from the user and then prints the ASCII letters between those two characters. 好的,我正在编写一个程序,该程序从用户读取两个字符,然后在这两个字符之间打印ASCII字母。 The problem is that when the program runs it prompts the user to enter the first character and once the user hits enter the program ends. 问题在于,程序运行时会提示用户输入第一个字符,一旦用户点击输入程序便结束。 What am I missing? 我想念什么?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){


      char firstchar;
      char secondchar;
      int variable;
      int highest;
      int lowest;
      char ASCIvariable;

      printf("Please enter a character. ");
      scanf("%d", &firstchar);

      printf("Please enter another character. ");
      scanf("%d", &secondchar);

      if(firstchar < secondchar) 
      {
         secondchar = highest;
         firstchar = lowest;
      }else{
         firstchar = highest;
         secondchar = lowest;
      }

      variable = lowest;

      for ( variable != highest; variable < highest; variable++ )
      {
          variable = ASCIvariable;
          printf(ASCIvariable);
      }

      return 0;
}

I clearly also don't understand how to post code on this site. 我显然也不明白如何在此网站上发布代码。 I need four spaces manually entered before EVERY line of code? 我需要在每行代码之前手动输入四个空格吗?


Update here is the current code also control k will not allow paste.... 更新这里是当前代码也控制k将不允许粘贴....

#include <stdio.h>
#include <stdlib.h>



 int main(int argc, char *argv[]) {


    char firstchar;
  char secondchar;
   int variable;
  int highest;
  int lowest;
  char ASCIvariable;

  printf("Please enter a character. ");
  scanf(" %c", &firstchar);

  printf("Please enter another character. ");
  scanf(" %c", &secondchar);

  if(firstchar < secondchar) 
  {
 highest = secondchar;
 lowest = firstchar;
}
  else
  {
  highest = firstchar;
  lowest = secondchar;
}

  variable = lowest;


 for (variable != highest; variable <= highest; variable++ )
  {
        ASCIvariable = variable;
      printf("%c ", ASCIvariable);

  }


    return 0;
}

It successfully allows the user to enter both characters and then prints the letters between the two. 它成功允许用户输入两个字符,然后在两个字符之间打印字母。 I think that is correct? 我认为是对的吗?

Change your code to: 将您的代码更改为:

printf("Please enter another character. ");
scanf(" %c", &secondchar);  /* Note the extra space and %d is changed to %c*/

Also change your for loop to: 还将您的for循环更改为:

for ( ; variable <= highest; variable++ ) /* Should be <= */ {
   ASCIvariable = variable; /* Reverse */
   printf("%c", ASCIvariable); /* %c */
}

Your assignations are also incorrect: 您的分配也不正确:

if(firstchar < secondchar) 
{
 highest = secondchar;
 lowest = firstchar;
}
else
{
  highest = firstchar;
  lowest = secondchar;
}

a = b; means copy contents of b into a . 表示将b内容复制到a

Using wrong format specifier might lead to UB. 使用错误的格式说明符可能会导致UB。 You need to scan a character 您需要扫描一个字符

scanf("%c", &firstchar);

Then flush the newline char using 然后使用刷新换行符

scanf(" %c",&secondchar);

The space before the %c consumes the newline char. %c之前的空格占用换行符。

1)You must get input with format specifier %c for characters 1)您必须使用格式说明符%c输入字符

2)You must consume the newline after entering first character 2)输入第一个字符后必须使用newline

3) You seem to be confused with assignment statements 3)您似乎对赋值语句感到困惑

a=b assigns the valure of b to a and not the other way around. a=b分配的valure ba ,而不是周围的其他方法。

printf("Please enter a character. ");
scanf(" %c", &firstchar);
//The space before %c will consume the newline

printf("Please enter another character. ");
scanf(" %c", &secondchar);

if(firstchar < secondchar) 
{
    highest=secondchar ;
    lowest=firstchar ;
}
else
{
    highest=firstchar;
    lowest= secondchar ;
}

//Changed the for loop to get characters between two inputs
variable = lowest+1;
for ( ; variable < highest; variable++ )
{
     ASCIvariable= variable ;
     printf("%c ", ASCIvariable);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM