提问人:Maulana 提问时间:10/3/2023 更新时间:10/3/2023 访问量:66
程序未显示 printf 语句
Program didnot show printf statement
问:
这段代码是关于根据之前在讲座中获得的值创建一个值,所以这里的重要变量只是 sks(credits)、b、sum、sumsksb 和 nh。该程序会将其处理成平均学分总和除以称为 IP 的学分数(rt 变量)的最终分数。但是我想在IP(rt)输出中显示先前输入的名称。 这是没有输出名称的代码:
#include <stdio.h>
int main(){
int i, N, kodemk, sks, b, sum, sumsksb, jsks;
char mk, nh, name;
float rt;
printf("Jumlah mata kuliah ");
scanf("%d", &N);
printf("Nama anda ");
scanf("%s", &name);
sum=0;
jsks = 0;
for(i=1; i<=N; i++){
printf ("Masukkan kode mata kuliah ");
scanf("%d", &kodemk);
printf("Masukkan Nama mata kuliah ");
scanf("%s", &mk);
printf("Masukkan jumlah sks ");
scanf("%d", &sks);
printf("Masukkan Nilai Huruf mata kuliah ");
scanf("%s", &nh);
if(nh == 'A'){
b = 4;
}
else if(nh == 'B'){
b = 3;
}
else if(nh == 'C'){
b = 2;
}
else if(nh == 'D'){
b = 1;
}
else if(nh == 'E'){
b = 0;
}
sumsksb = sks * b;
sum = sum + sumsksb;
jsks = jsks + sks;
}
rt = sum/jsks;
printf("IP %.2f ", rt);
}
如果我运行它,最后会出现IP(rt)的printf输出,输出没有问题
但是当我想打印这个名字时
#include <stdio.h>
int main(){
int i, N, kodemk, sks, b, sum, sumsksb, jsks;
char mk, nh, name;
float rt;
printf("Jumlah mata kuliah ");
scanf("%d", &N);
printf("Your Name ");
scanf("%s", &name);
sum=0;
jsks = 0;
for(i=1; i<=N; i++){
printf ("Masukkan kode mata kuliah ");
scanf("%d", &kodemk);
printf("Masukkan Nama mata kuliah ");
scanf("%s", &mk);
printf("Masukkan jumlah sks ");
scanf("%d", &sks);
printf("Masukkan Nilai Huruf mata kuliah ");
scanf("%s", &nh);
if(nh == 'A'){
b = 4;
}
else if(nh == 'B'){
b = 3;
}
else if(nh == 'C'){
b = 2;
}
else if(nh == 'D'){
b = 1;
}
else if(nh == 'E'){
b = 0;
}
sumsksb = sks * b;
sum = sum + sumsksb;
jsks = jsks + sks;
}
rt = sum/jsks;
printf("Nama:\n%s", name);
printf("IP %.2f ", rt);
}
name 和 rt 的输出未显示,程序在显示输出之前结束
如何显示名称?
答:
0赞
SmellyCat
10/3/2023
#1
令人惊讶的是,没有报告内存访问冲突。
声明应该读得更像:
char mk[4], name[76]; /* allows 3 character mata code
and name up to 75 characters */
char nh;
如果保留一个字符,则其语句需要将其格式更新为 。nh
scanf
%c
scanf("%c", &nh);
这应该会降低崩溃的可能性。
评论
1赞
Maulana
10/3/2023
@RetiredNinja这是可行的,感谢大家
0赞
Anish Bandal
10/3/2023
#2
获取 name 变量的数组并尝试
0赞
Maulana
10/3/2023
#3
尝试您的建议后,此代码有效,谢谢
#include <stdio.h>
void main(){
int i, N, kodemk, sks, b, sum, sumsksb, jsks;
char nh;
char nama[67], mk[10];
float rt;
printf("Jumlah mata kuliah ");
scanf("%d", &N);
printf("Nama anda ");
scanf("%s", &nama);
sum=0;
jsks = 0;
for(i=1; i<=N; i++){
printf ("Masukkan kode mata kuliah ");
scanf("%d", &kodemk);
printf("Masukkan Nama mata kuliah ");
scanf("%s", &mk);
printf("Masukkan jumlah sks ");
scanf("%d", &sks);
printf("Masukkan Nilai Huruf ");
scanf(" %c", &nh);
if(nh == 'a' ){
b = 4 * sks;
}
else if(nh == 'b'){
b = 3 * sks;
}
else if(nh == 'c'){
b = 2 * sks;
}
else if(nh == 'd'){
b = 1 * sks;
}
else if(nh == 'e'){
b = 0 * sks;
}
sumsksb = b;
sum = sum + sumsksb;
jsks = jsks + sks;
}
rt = sum/jsks;
printf("Nama: %s\n", nama);
printf("IP %.2f ", rt);
}
评论
name
name
是单个字符,不能与说明符一起使用。也许你想做一个字符数组?看起来您对其他一些变量也有同样的问题。%s
name
name
mk
nh
char
scanf
scanf(" %c", &nh);
%c
%c
scanf
name
mk
char
char name[100];
char mk[100];
scanf("%99s", name);
scanf("%99s", mk);
char
%s
scanf(" %99[^\n]", name);