【问题标题】:Variable Amount of Input on the Same Line in CC中同一行上的可变输入量
【发布时间】:2020-02-13 18:18:13
【问题描述】:

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

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

#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 个字符并单击 Enter 时,而不是打印“你想要多少个孩子?”它只是空白。

【问题讨论】:

  • 对于初学者来说,index1 + 1 不会更新 index。您需要将其更改为 index = index + 1index++。但请编辑您的问题并提供MCVEplayNummarrykidsmoney 是什么?
  • scanf 缺点 #7:%c 不同。您可能只需要使用" %c"(注意前导空格)。
  • “它编译”但并非没有警告。 MSVC 说 '+': operator has no effect;具有副作用的预期运算符。它仍然在第一个循环中,没有推进控制变量。
  • 使 for 循环有 index1++ 的帮助," %c" 也有空格,非常感谢你们,这是我在这里的第一篇文章,帮助很棒。你们在 20 分钟内完成了我过去 3 天无法完成的事情。

标签: c arrays computer-science


【解决方案1】:

通过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.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多