为什么我的预期输出没有发生?

时间:2011-04-19 03:34:10

标签: c++

   /*This program will ask a user to enter a song list. By doing this each time that the user enters
a song, he/she will then be asked if they want to enter another song, if yes then they will enter another song.There will be a maximum of 15 songs.
This progarm will help a user keep track of their songs. But then the program will output the amount of songs, and what they are.
input:A song. Then a yes or no, if yes, another song, if no the program will output.
output:The number of songs, and then the songs themselves, each song with it's own individual line.
processing: There will be two functions, one of which will ask the user for the songs and store them, and another for which will be computing
the output.
*/
    #include<iostream>
    #include<string>

    using namespace std;

    void input(string[15], int);
    void printArray(string[15], int);

    int main()
    {
    string songarray[15];
    int arraysize=15;
    int j=0;

    input(songarray, arraysize);

    int x=songarray[j].length();

    cout<<"There were "<<x<<" song titles entered.";
    printArray(songarray, x);
    return 0;
    }

/*This function will ask for the users input for a song and store the song in the songarray. He or she
will then be asked whether or not they want to enter another song, if so, then they will enter another song.
input:a song, y/Y or n/N for if they want to continue or not with another song
output:song will be sent to main function
processing: A while loop will be used for the user to enter is Y or N, and a for loop (while loop nested) for the user to enter the 
songs
*/
    void input(string titles[15], int rows)
    {

    char answer='y';

    for(int i=0;i<rows && (answer=='Y' || answer=='y');i++)
    {

        cout<<"Enter the name of a song. ";
        cin>>titles[i];
        cout<<"Do you want to continue (y/n)? ";
        cin>>answer;
    }
    }

/*The purpose of this function is to print the array from the main function.
input:accepts the array from the main function
output: prints the array
processing:nested loops will pring this array.
*/

    void printArray(string playlist[15], int quantity)
    {
    for(int j=0; j<quantity; j++)
    {
        cout<<playlist[j];
    }
    }

所以预期输出是清楚的,程序要求输入一首歌,然后要求用户输入y或n表示是或否如果他们想要输入另一首歌。如果是,则程序再次询问。但是最多是15首歌曲,最后如果用户输入n或者到15,则程序将输出“已输入空白歌曲标题”,其中空白是输入的歌曲数量。我要么没有输出,输入0首歌曲,和/或没有接近我想要的输出,我的问题是什么?/

1 个答案:

答案 0 :(得分:1)

在...

songarray[j].length();

... j已被设置为0,因此它会询问第一首歌曲标题的长度(以字符为单位),而不是数组中已填充的歌曲标题的数量(您没有保留完全跟踪。)

建议:

  • 为您的歌曲标题使用std::vector<std::string>,并在阅读时使用push_back()个新歌曲标题。然后,您无需担心或担心程序支持的歌曲数量限制(15)。您还可以使用.size()告诉您已完成的推送次数。
  • 检查流式传输后的std::cin状态
  • std::cin >> some_std_string将停留在第一个空格,但真实世界的歌曲标题有多个单词:您应该使用std::getline(std::cin, variable)
  • 考虑使用空行作为输入分隔符,而不是每次都询问是/否,这样会更容易:

总而言之,请考虑将其置于程序的核心并从那里开始工作......

std::vector<std::string> song_titles;
std::string next_song;
while (std::getline(std::cin, next_song) && next_song != "")
    song_titles.push_back(next_song);

注意:有些人可能更喜欢!next_song.empty(),因为它更有效率,但我觉得它也不那么富有表现力。