Hello, and thank you for taking the time to help me out! I am working on my first real C++ program, and have the code almost entirely written. The assignment is to write four files, CPU.h, CPU.cpp, Computer.h, and Computer.cpp, which maintain information about an object (a computer), and then to use a given file, Assignment6.cpp, to interact with the user.
I'm just having some troubles with the actual input and output of the program, because when I select option A, which is to add a computer, and then enter the first information about the computer, which is the brand name, after I hit enter the program displays the next 4 prompts without waiting for user input. I think there may be a problem with the given file, Assignment6, in which case I need to talk to the professor, but I just want to make sure that there is nothing wrong with the files I have written. Can someone help me figure out why the code is doing this, and not allowing the user to input values for the next four prompts?
Also, those of you who are really into looking at details will notice that I have just one function left to write, which is the setCPU function in the Computer.cpp file. I am unsure about how to access the variables from CPU.cpp in Computer.cpp, so if someone could help me figure that out as well that would be great!
Thank you in advance for any help you can provide!!
Here are my five files:
// Computer.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "Computer.h"
#include "CPU.h"
using namespace std;
// constructor
Computer::Computer()
{
brandName = "?";
*cpu = CPU();
memory = 0;
price = 0.0;
}
// destructor
Computer::~Computer()
{
delete cpu;
cout << "The computer " << brandName << " is being destroyed." << endl;
}
// accessor functions return the variables
string Computer::getBrandName()
{ return brandName; }
CPU * Computer::getCPU()
{ return cpu; }
int Computer::getMemory()
{ return memory; }
double Computer::getPrice()
{ return price; }
// mutator functions set the variables to the new values
void Computer::setBrandName(string newBrandName)
{ brandName = newBrandName; }
void Computer::setCPU (string cpuType, int cpuSpeed)
{
}
void Computer::setMemory (int memoryAmount)
{ memory = memoryAmount; }
void Computer::setPrice (double newPrice)
{ price = newPrice; }
// printInfo function prints the information about the computer in the
// desired format
void Computer::printInfo()
{ cout << "\nBrandName:\t" << brandName << "\nCPU:\t\t" << cpu << "\nMemory:\t\t" << memory << "\nPrice:\t\t" << price << "\n\n" << endl; }
// Computer.h
// protectors
#ifndef Computer_H
#define Computer_H
using namespace std;
// CPU class so that everything compiles correctly
class CPU;
class Computer
{
// private variables
private:
string brandName;
CPU *cpu;
int memory;
double price;
// public functions
public:
Computer();
~Computer();
string getBrandName();
CPU * getCPU();
int getMemory();
double getPrice();
void setBrandName(string newBrandName);
void setCPU (string cpuType, int cpuSpeed);
void setMemory (int memoryAmount);
void setPrice (double newPrice);
void printInfo();
};
#endif
// CPU.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "CPU.h"
using namespace std;
// constructor
CPU::CPU()
{
type = "?";
speed = 0;
}
// destructor
CPU::~CPU()
{ cout << "CPU with the type " << type << " and the speed " << speed << " is being destroyed." << endl; }
// accessor functions return the variables
string CPU::getType()
{ return type; }
int CPU::getSpeed()
{ return speed; }
// mutator functions set the variables to the new values
void CPU::setType(string newType)
{ type = newType; }
void CPU::setSpeed(int newSpeed)
{ speed = newSpeed; }
// printInfo function prints the information about the CPU in the
// desired format.
void CPU::printInfo()
{ cout << type << "," << speed << endl; }
// CPU.h
// protections
#ifndef CPU_H
#define CPU_H
using namespace std;
class CPU
{
// private variables
private:
string type;
int speed;
// public functions
public:
CPU();
~CPU();
string getType();
int getSpeed();
void setType(string type);
void setSpeed(int speed);
void printInfo();
};
#endif
#include <iostream>
#include <string>
#include <cstdlib>
#include "CPU.h"
#include "Computer.h"
using namespace std;
void printMenu();
int main()
{
// local variables, can be accessed anywhere from the main method
char input1 = 'Z';
string inputInfo;
string brandName;
double price;
int memory;
string cpuType;
int cpuSpeed;
string line;
// allocate memory for the pointer of the Computer
Computer * computer1 = new Computer();
printMenu();
do // will ask for user input
{
cout << "What action would you like to perform?\n";
input1 = getchar();
input1 = toupper(input1);
// matches one of the case statement
switch (input1)
{
case 'A': //Add Computer
cout << "Please enter the computer information:\n";
cout << "Enter a brand name:\n";
cin >> brandName;
computer1->setBrandName(brandName);
cout << "Enter a computer price:\n";
cin >> price;
computer1->setPrice(price);
cout << "Enter a computer memory:\n";
cin >> memory;
computer1->setMemory(memory);
cout << "Enter a cpu type:\n";
cin >> cpuType;
cout << "Enter a cpu speed:\n";
cin >> cpuSpeed;
computer1->setCPU(cpuType, cpuSpeed);
break;
case 'D': //Display computer
computer1->printInfo();
break;
case 'Q': //Quit
delete computer1;
break;
case '?': //Display Menu
printMenu();
break;
default:
cout << "Unknown action\n";
break;
}
getchar(); //to flush '\n'
} while (input1 != 'Q');
return 0;
}
/** The method printMenu displays the menu to a user**/
void printMenu()
{
cout << "Choice\t\tAction\n";
cout << "------\t\t------\n";
cout << "A\t\tAdd Computer\n";
cout << "D\t\tDisplay Computer\n";
cout << "Q\t\tQuit\n";
cout << "?\t\tDisplay Help\n\n";
}