2011-04-19 67 views
0
/*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,如果他們想要輸入另一首歌曲,請輸入yes或no。如果是,那麼程序再次提問。但是最大值是15首歌曲,最後如果用戶輸入n或15進入,程序將輸出「有空白歌曲標題輸入」,其中空白是輸入的歌曲數量。我一直在要麼沒有得到輸出,0首歌曲進入和/或沒有靠近輸出我想要的,什麼是我的問題?/爲什麼我的預期輸出不會發生?

+3

這看起來像是家庭作業或測試問題。 – 2011-04-19 03:38:30

+0

等一下,你每次都得到不同的結果?你說'和/或',所以我不太理解你的問題。你能否澄清一點? – Blender 2011-04-19 03:38:53

+0

您可能會發現以下閱讀有幫助:http://www.cprogramming.com/tutorial/references.html – Anton 2011-04-19 04:00:40

回答

1

在...

songarray[j].length(); 

... j一直設置爲0,所以它要求第一首歌曲標題的長度(以字符爲單位),而不是數組中已填充的歌曲標題的數量(根本沒有記錄)。

建議:

  • 使用std::vector<std::string>你的歌曲名稱,並作爲他們閱讀push_back()片頭新曲。那麼你不需要擔心你的程序支持的歌曲數量有限制(15)。您還可以使用.size()來告訴您已完成多少次push_back。
  • 檢查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()因爲它是更有效的,但我覺得它不太表現了。

+1

好點,但關於「通過引用傳遞」的觀點是錯誤的。 'songarray'是一個數組,當傳遞給函數時會衰減爲一個指針。 – 2011-04-19 04:03:05

+0

@Alok:噢,對我來說多麼愚蠢。謝謝。 – 2011-04-19 04:06:24

相關問題