写一个简单的程序 [英] Write a simple program

查看:74
本文介绍了写一个简单的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写程序以输入名称和卷号以及两个主题的标记,然后找到总数和百分比,然后打印名称,卷号,总数和百分比。



我尝试了什么:



Write a Program to enter name and roll number and marks of two subject and then find the total and percentage then print the name , roll number, total and percent.

What I have tried:

/* Program Of School */
#include <stdio.h>
void main ()
{
 char Name[50];
 Int Roll, Mark1, Mark2;
 float per;
Printf("enter the name and roll number\n");
scanf("%c %d" , &name, &Roll);
printf("enter the marks of two subject");
scanf("%d %d" , &mark1, &mark2);
sum=mark1+mark2:
per=(mark1+mark2)/2.0;
printf("the name is %c and roll mo is %d, &name, &roll number);
printf("total marks are %d and percentage are %f, &sum, &per);
getch();
}

推荐答案

四个问题:


  1. scanf()您必须将指针作为参数传递以获取存储的值。但是使用 printf(),你必须传递整数值,如 int float )按值。

  2. %c格式序列适用于单个字符。使用%s获取字符串。

  3. 使用字符串( char arrays)对象已经是一个指针。所以你必须传递普通名称或第一个元素的地址(名称& Name [0] )。

  4. 您的百分比计算错误。通用公式是 100 * part / total


  1. With scanf() you have to pass pointers as arguments to get the values stored. But with printf() you have to pass integral values like int and float) by value.
  2. The "%c" format sequence is for a single character. Use "%s" for strings.
  3. With strings (char arrays) the object is already a pointer. So you have to pass the plain name or the address of the first element (Name or &Name[0]).
  4. Your percentage calculation is wrong. The general formula is 100 * part / total.


你的程序充满了bug。在编写 C 程序时必须小心:

  • C 区分大小写,如果您将变量命名为 Mark1 ,则每次出现必须 完全 Mark1 ('mark1'不是一个选项)。同样地,有一个 printf 函数,而不是'Printf'。
  • scanf 接受地址的参数,而 printf 获取参数的
  • 语句终止by semicolon(; )而不是冒号(':')。
  • 字符串的格式说明符是%s 不是'%c'。
  • 您错过了最后两个 printf 来电
  • 中格式说明符的尾随双引号
You prograsm is full of bugs. You have to be careful while writing a C program:
  • C is case-sensitive, if you name a variable Mark1 then every its occurrence must be written exactly Mark1 ('mark1' is not an option). Likewise there is a printf function, not a 'Printf'.
  • scanf takes the address of the arguments whereas printf takes the value of the arguments.
  • The statement are terminated by semicolon (;) not colon (':').
  • The format specifier of strings is %s not '%c'.
  • You missed the trailing double quote of the format specifier in the last two printf calls
#include <stdio.h>
int main ()
{
  char name[50];
  int roll, mark1, mark2;
  int sum;
  double avg;
  printf("enter the name and roll number\n");
  scanf("%s %d" , name, &roll);
  printf("enter the marks of two subject\n");
  scanf("%d %d" , &mark1, &mark2);
  sum = mark1 + mark2;
  avg = (mark1 + mark2)/2.0;
  printf("the name is %s and roll mo is %d\n", name, roll );
  printf("total marks are %d and the average is %g\n", sum, avg);
  getchar();
  return 0;
}


除非您的主题只有单个字符名称,否则无效。

对于scanf, %c表示读取单个字符,因此如果输入mike,它将全部失败。

相反,使用%s读取字符串,并提示用户输入单独的行数据:首先提示输入名称,读取字符串,然后提示输入卷号并读取。

我还建议您对标记执行相同的提示,然后阅读标记 - 这样可以让用户更容易理解。
Won't work, unless your subjects only ever have single character names.
For scanf, "%c" means "read a single character" so if you type "mike" it will all fail.
Instead, use "%s" which reads a string, and prompt the user for separate lines worth of data: first prompt for the name, read the string, then prompt for the roll number and read that.
I'd also suggest that you do the same "prompt for one item, then read it" for the marks - it makes it easier for the user to follow.


这篇关于写一个简单的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆