c - 在 C 中将用户输入存储在数组中并将其打印回用户

标签 c arrays string pointers

我希望能够要求用户输入 12 个字符和整数,然后将其打印回给用户。字符是音符,整数是每个音符的长度(以秒为单位)。

这是我的代码:

//Include Standard IO
#include <stdio.h>
#include <string.h>

//----------
//----------Main Function----------
//----------


int main()
{

//Request Notes off of the user
printf(" Please enter a musical sequence of 15 notes in capitals: ");
printf("\n Please use the # character if needed");
printf("\n Please enter the time you want each note to last in seconds");


//Declare and initialize a table of notes as chars
int notelist[12];
int*ptr;
ptr = notelist;
//Creating the array for the single notes
char note[3];
//Creating a variable for the beginning of the note
int start;
//Declare and initialize a table of integers for the length of each note
int notelength[20];
//Creating a variable for the length of the note
int time;
//Creating a variable for the loop so that it runs 15 times
int loop;


    //Creating a loop that will run 15 times for the 15 note values
    for(loop=1; loop<4; loop++)

    {
        //Request a note off the user
        printf("\n\n Please enter a note in capitals: ");
        //Taking the input from the user
        scanf("%s", note);

        //Ask the user for the length he/she wants each note to be
        printf("\n Please enter the length you want each note to be in seconds: ");
        //Taking the input from the user
        scanf("%d", &time);
        notelength[time]=time+1;
    }

    //Print the input from the user back
    for(start=1; start<4; start++)
    {
        scanf("%d", &*ptr++);
    }

    for(start=1; start<4; start++)
    {
        printf("%d", *ptr++);
    }


//Return 0 to let the OS know the program finished successfully
return 0;
}

如何存储用户输入的 12 个音符和长度,并在循环结束时将其打印回给他们?

最佳答案

您需要一个动态分配的堆栈来存储稍后可以打印的所有笔记。任何数据结构书籍或在线教程都会有帮助。

关于c - 在 C 中将用户输入存储在数组中并将其打印回用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13348698/

相关文章:

sql - array_agg() 函数的返回类型

java - 查找每一行的整数和字符串并将它们放入数组中?

c# - 如何分隔字符串中的段落?

c - Linux 编程 - 先进先出

arrays - 将嵌入空格的文件名读取到 shell 脚本中的数组中

c - GTKSDL - 处理事件

java - 如何在java的控制台输出中打印带下划线的字符串

c++ - 为什么数组中的数据被删除后还能用?

c - GCC 错误地优化了自定义地址处变量的指针相等测试

c - C 中 printf() 的意外工作