我試圖創建一個程序,提示用戶輸入字符串以創建一個非常簡單的動畫。我的代碼工作正常,但是,當我試圖顯示字符串,我不能完全得到它的工作(第二個功能)請幫助!如何遍歷整個字符串並一次顯示一個字符C++
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void getAnimation(string& animation);
void displayAnimation(string& animation);
int main()
{
string animation;
getAnimation(animation);
displayAnimation(animation);
return 0;
}
//Ask user to enter a single frame
//returned as string
//Input Parameters: Numeber of Frames, Number of Lines, and Animation
//Returns: The animation (string)
void getAnimation(string& animation)
{
int counter, lines, i, numLines, frameNum;
cout << "How many frames of animation would you like?" << endl;
cin >> numLines;
// numbers to help with for - loop
counter = 0;
frameNum = 1;
//for - loop to get information
for (counter = 0; counter < numLines; counter++)
{
cout << "How many lines do you want in this frame?" << endl;
cin >> lines;
for (i = 0; i < lines; ++i)
{
cout << "Give me line of frame #" << frameNum++ << endl;
cin >> animation;
//getline(cin, animation);
//cin.ignore();
}
}
}
//Will gather the string received in main and gather it here to display
//Returned full animation
//Input Parameters: None
//Output Parameters: Animation
void displayAnimation(string& animation)
{
cout << "Here is your super-sweet animation!\n";
// to print out entire animation
//for (auto c : animation)
//cout << animation << endl;
//for (int i = 0; i < animation.length(); i++);
}
什麼確切的問題正在面臨着? –
該程序只輸出我輸入的最後一個字符,而不是整個字符串。 –
在'getAnimation'內的'animation'中放置的最後一個值是返回到'main()'的那個值,然後在那裏將其傳遞給'displayAnimation'。如果這不是你的意圖,請更改代碼。 – WhozCraig