gpt4 book ai didi

c - C中同一行的可变输入量

转载 作者:太空狗 更新时间:2023-10-29 14:49:24 24 4
gpt4 key购买 nike

我必须扫描同一行以获得数组的可变输入量(用户选择他们想要使用的输入数量)。我必须做一个字符,一个整数和一个 float 。

我已经尝试了 for 循环,并在 foor 循环内使用带有嵌套循环和语句的空格做了不同的事情,但没有任何效果。编辑这不是我的全部代码,我添加了更多代码,以便人们更好地了解我正在尝试做什么。

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

int main() {
int ranNum1, ranNum2, ranNum3, ranNum4, playNum, ranPlay;
int index1, index2, index3;

printf("How many choices do you want to play with? ");
scanf("%d", &playNum);
srand(time(0));
ranPlay = playNum + 1;
ranNum1 = rand() % ranPlay;
ranNum2 = rand() % ranPlay;
ranNum3 = rand() % ranPlay;
ranNum4 = rand() % 5;
int kids[playNum];
char marry [playNum];
float money [playNum];
printf("Who do you want to marry?\n");
printf("(enter %d first name initials) ", playNum);
for (index1 = 0; index1 < playNum; index1 + 1) {
scanf("%c", &marry[index1]);
}
printf("How many kids do you want?\n");
printf("(enter %d whole numbers) ", playNum);
for (index2 = 0; index2 < playNum; index2 + 1) {
scanf("%d", &kids[index2]);
}
printf("How much money do you want?\n");
printf("(enter %d decimal amounts) ", playNum);
for (index3 = 0; index3 < playNum; index3 + 1) {
scanf("%f", &money[index3]);
}
}

它编译,只是当我运行它时它运行不正确,当我输入 3 个字符并单击回车时,而不是打印“你想要多少个 child ?”它只是空白。

最佳答案

通过 gcc 编译器运行最新发布的代码会产生:

gcc    -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11  -c "untitled.c"  
untitled.c: In function ‘main’:
untitled.c:10:11: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
srand(time(0));
^~~~
untitled.c:10:11: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
untitled.c:21:47: warning: statement with no effect [-Wunused-value]
for (index1 = 0; index1 < playNum; index1 + 1) {
~~~~~~~^~~
untitled.c:26:47: warning: statement with no effect [-Wunused-value]
for (index2 = 0; index2 < playNum; index2 + 1) {
~~~~~~~^~~
untitled.c:31:47: warning: statement with no effect [-Wunused-value]
for (index3 = 0; index3 < playNum; index3 + 1) {
~~~~~~~^~~
untitled.c:5:36: warning: variable ‘ranNum4’ set but not used [-Wunused-but-set-variable]
int ranNum1, ranNum2, ranNum3, ranNum4, playNum, ranPlay;
^~~~~~~
untitled.c:5:27: warning: variable ‘ranNum3’ set but not used [-Wunused-but-set-variable]
int ranNum1, ranNum2, ranNum3, ranNum4, playNum, ranPlay;
^~~~~~~
untitled.c:5:18: warning: variable ‘ranNum2’ set but not used [-Wunused-but-set-variable]
int ranNum1, ranNum2, ranNum3, ranNum4, playNum, ranPlay;
^~~~~~~
untitled.c:5:9: warning: variable ‘ranNum1’ set but not used [-Wunused-but-set-variable]
int ranNum1, ranNum2, ranNum3, ranNum4, playNum, ranPlay;
^~~~~~~
Compilation finished successfully.

不要让编译器发出最后的消息:“编译成功完成。”骗你。这只是编译器说它针对代码中的问题实现了某种(通常是错误的)解决方法。

关于c - C中同一行的可变输入量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58422252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com