Hello, I have an assignment due in about an hour and I have it working, there's just things going on with it I'm stuck on how to fix:
1. I have a part of the program where I want it to accept a 'Y' as user input for creating something on a list. I can get it to do that, but when I type something else other than Y to stop it, it doesn't do such thing. I want it to display the user input of the list upon an 'N' value.
2. When I have it inputting values, it displays two at the same time and I can't input them separately. For example, this is a program where I input my ranking, title, and genre of a video game, I have it set up to be input in that order but it asks for BOTH the ranking and name at the same time, I don't want it to do that.
It works and writes to files how I want it to be, but the issues above are preventing me from inputting it properly.
Here's my code:
FavoriteThing.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include "stdafx.h"
using namespace std;
class FavoriteThing
{
private:
int rating;
string name;
string genre;
public:
FavoriteThing();
FavoriteThing(string n, int r, string g);
void Display();
int CaptureThing();
friend ofstream& operator<<(ofstream &fs, FavoriteThing *thing);
};
|
FavoriteThing.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "stdafx.h"
#include "FavoriteThing.h"
using namespace std;
void FavoriteThing::Display()
{
cout << " ========= Favorite Things ========= " << endl;
cout << "My ranking: " << "#" << rating << endl;
cout << "The name of it is: " << name << endl;
cout << "It is a(n) " << genre << " game." << endl;
}
int FavoriteThing::CaptureThing()
{
cout << "Input game name: " << endl;
getline(cin, name);
cout << "What is your ranking of it? (from #1, etc.) " << endl;
cin >> rating;
cout << "Input genre: " << endl;
getline(cin, genre);
return 0;
}
FavoriteThing::FavoriteThing()
{
name = "";
rating = 0;
genre = "";
}
FavoriteThing::FavoriteThing(string n, int r, string g)
{
name = n;
rating = r;
genre = g;
}
ofstream& operator<<(ofstream &fs, FavoriteThing *thing)
{
fs << thing->name << "," << thing->rating << "," << thing->genre << endl;
return fs;
}
|
Main program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
// INFO450FavoriteThing.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include "stdafx.h"
#include "FavoriteThing.h"
using namespace std;
const int THINGSIZE = 100;
int main()
{
FavoriteThing **myThing;
int count = 0;
int t;
string input = "Y";
myThing = new FavoriteThing*[THINGSIZE];
// Read items to a file
ifstream datafile("c:\\users\\adamransom\\Documents\\thing.txt");
if (datafile.good())
{
while (!datafile.eof())
{
string name, rank, genre;
getline(datafile, name, ',');
if (name.length())
{
getline(datafile, rank, ',');
getline(datafile, genre, '\n');
myThing[count] = new FavoriteThing(name, stoi(rank), genre);
count++;
}
}
}
cout << "My favorite thing is video games!\n" << endl;
cout << "There are " << count << " game entries. Do you want to input more?" << endl;
cin >> input;
// Count inventory items
while (input == "Y" || input == "y")
{
myThing[count] = new FavoriteThing();
if (myThing[count]->CaptureThing() == 0)
count++;
cout << "Are you entering another?" << endl;
cin >> input;
}
for (t = 0; t < count; t++)
{
myThing[t]->CaptureThing();
}
// Save data entries to a text file
ofstream outfile("c:\\users\\adamransom\\Documents\\thing.txt");
if (!outfile.good())
{
cout << "ERROR: Cannot open file! Try again." << endl;
return -1;
}
for (t = 0; t < count; t++)
{
outfile << myThing[t];
}
// To clean up the memory after
for (t = 0; t < count; t++)
{
delete myThing[t];
}
delete myThing;
return 0;
}
|