将用户输入存储在阵列中并将其打印回C中的用户

时间:2012-11-12 17:44:25

标签: 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个音符和长度,并在循环结束后将其打印回来?

1 个答案:

答案 0 :(得分:0)

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