c++ - 数据未存储以显示?

标签 c++

我是一名正在上编程课的初学者,我在作业中遇到了问题。我要做一个可以存储和排序数据的程序(我选择了游戏),一切似乎都很顺利。除了当我选择输入游戏,然后显示我输入的游戏时,列表中不会有任何内容。有什么我想念的吗?

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

struct VidyaGames {
string Title;
string Date;
string Developer;
};

void getGames(VidyaGames array[], int &k);
void displayGames(VidyaGames array[], int &k);
void deleteGames(VidyaGames array[], int &k);
void sortGames(VidyaGames array[], int &k);



const int MAX = 150;

string Title;
string Date;
string Developer;

int main()
{
char choice;
VidyaGames array[MAX];
bool kek = true;
int k = 0;

do

{

    cout << "Welcome to the 'Super Incredible Amazing Game Storage-o-Tron 5000(and one)." << endl;
    cout << " " << endl;
    cout << "Please select which task you would like to perform by typing in the " << endl;
    cout << "corresponding letter in the bracket: " << endl;
    cout << " " << endl;
    cout << "[I]nput a game into the list." << endl;
    cout << "[D]isplay the games you have stored." << endl;
    cout << "[S]ort the games you have stored." << endl;
    cout << "[R]emove a game from the list." << endl;
    cout << "[Q]uit the program." << endl;
    cin >> choice;

    switch (choice)
    {
        case 'I': getGames(array, k); break;
        case 'D': displayGames(array, k); break;
        case 'S': deleteGames(array, k); break;
        case 'R': deleteGames(array, k); break;
        case 'Q': kek = false; break;
        default : cout << "Hey. Remember when I gave you the specific      options you were allowed to choose?" << endl;
                   cout << "Maybe enter one of those?" << endl;
                   cout << " " << endl;
        }
        }
while (kek);
cout << "You have killed me." << endl;

}
void getGames(VidyaGames array[], int &k)
{
system("cls");
VidyaGames tmp;
char lel[100];
cout << "Enter the title of your game: " << endl;
getline (cin, Title);
cout << "Enter the date your game was published: (Example: March 15th, 2014)" << endl;
getline (cin, Date);
cout << "Enter the developer of your game: " << endl;
getline (cin, Developer);

}

void displayGames(VidyaGames array[], int &k)
{
system ("cls");
if (k==0)
    cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;


else if (k > 0) {
for (int i=0; i < 0; i++)
{
    cout << "Title: " << array[i].Title << endl;
    cout << "Release Date: " << array[i].Date << endl;
    cout << "Developer: " << array[i].Developer << endl;
     }
}
 }


   void deleteGames(VidyaGames array[], int &k) {


system("cls");
char deleteChoice;
if (k==0)
    cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
else {
    cout << "Please type the name of the game you would like to delete: " << endl;
    cin >> deleteChoice;

}
}

   void sortGames(VidyaGames array[], int &k)
   {

   }

最佳答案

您忘记在数组中设置值。

我向函数 getGames 添加了 4 行:

array->Title = Title;
array->Date = Date;
array->Developer = Developer;
k++;

并在函数 displayGames 中更改一行:

for (int i=0; i < k; i++)

这是最终代码:

// test_3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

struct VidyaGames {
    string Title;
    string Date;
    string Developer;
};

void getGames(VidyaGames array[], int &k);
void displayGames(VidyaGames array[], int &k);
void deleteGames(VidyaGames array[], int &k);
void sortGames(VidyaGames array[], int &k);



const int MAX = 150;

string Title;
string Date;
string Developer;

int main()
{
    char choice;
    VidyaGames array[MAX];
    bool kek = true;
    int k = 0;

    do

    {

        cout << "Welcome to the 'Super Incredible Amazing Game Storage-o-Tron 5000(and one)." << endl;
        cout << " " << endl;
        cout << "Please select which task you would like to perform by typing in the " << endl;
        cout << "corresponding letter in the bracket: " << endl;
        cout << " " << endl;
        cout << "[I]nput a game into the list." << endl;
        cout << "[D]isplay the games you have stored." << endl;
        cout << "[S]ort the games you have stored." << endl;
        cout << "[R]emove a game from the list." << endl;
        cout << "[Q]uit the program." << endl;
        cin >> choice;

        switch (choice)
        {
        case 'I': getGames(array, k); break;
        case 'D': displayGames(array, k); break;
        case 'S': deleteGames(array, k); break;
        case 'R': deleteGames(array, k); break;
        case 'Q': kek = false; break;
        default : cout << "Hey. Remember when I gave you the specific      options you were allowed to choose?" << endl;
            cout << "Maybe enter one of those?" << endl;
            cout << " " << endl;
        }
    }
    while (kek);
    cout << "You have killed me." << endl;

}
void getGames(VidyaGames array[], int &k)
{
    system("cls");
    VidyaGames tmp;
    char lel[100];
    cout << "Enter the title of your game: " << endl;
    getline (cin, Title);
    cout << "Enter the date your game was published: (Example: March 15th, 2014)" << endl;
    getline (cin, Date);
    cout << "Enter the developer of your game: " << endl;
    getline (cin, Developer);

    array->Title = Title;
    array->Date = Date;
    array->Developer = Developer;
    k++;


}

void displayGames(VidyaGames array[], int &k)
{
    system ("cls");
    if (k==0)
        cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;


    else if (k > 0) {
        for (int i=0; i < k; i++)
        {
            cout << "Title: " << array[i].Title << endl;
            cout << "Release Date: " << array[i].Date << endl;
            cout << "Developer: " << array[i].Developer << endl;
        }
    }
}


void deleteGames(VidyaGames array[], int &k) {


    system("cls");
    char deleteChoice;
    if (k==0)
        cout << "There is literally nothing in this list, as you have made the mental choice to not put anything in it yet." << endl;
    else {
        cout << "Please type the name of the game you would like to delete: " << endl;
        cin >> deleteChoice;

    }
}

void sortGames(VidyaGames array[], int &k)
{

}

关于c++ - 数据未存储以显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36561380/

相关文章:

c++ - 如何找到多项式方程的系数?

c++ - 如何在单元测试中访问私有(private)成员?

c++ - 如何正确删除C++中的指针?

c++ - 如何在3x3网格的空白处输入2或4?

c++ - 使用 Doxygen 为外部文件生成 doc

c++ - 具有不同参数的函数容器

c++ - 将 int* 转换为字符串,然后将字符串转换为 int*

c++ - 使用 boost 的 program_options 时,如何确保声明在 C++ 中具有存储类或类型说明符?

c++ - 在协程执行之间强制休眠

c++ - 无法将本地编译的库添加到 CMake 项目