I'm trying to create a simple program that acts like an ATM for learning purposes. I have a working program so far, but now I want to "take it to the next level" if you will and import my banks balance from a text/csv file and then assign to a variable, if any changes are made to the balance I also want to write those changes to said text file. I've searched the web so far and haven't been able to find much information that seems particularly relevant to my question.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//calculates money withdrawal.
int main(){
double currentBalance = -250.63;
double prevBalance = currentBalance;
double withdrawal = 0;
double deposit = 0;
double deficit = currentBalance;
double updatedWithdrawal = 0; //(currentBalance - withdrawal)
double updatedDeposit = 0; //(currentBalance + deposit)
string answer = "blah";
cout << "Welcome to bank bro's banking services.\nWhat would you like to do? Withdrawal, Deposit, or View Account Status(VAS): ";
cin >> answer; // asks the user if they intend to withdrawal, or deposit.
if (answer == "VAS"){
if (currentBalance > 0)
cout << "\n Your account is in good standing." << endl;
else{
cout << "\n You currently owe: $" << deficit << endl;
}
cout << "\n Your current balance is: $" << currentBalance << endl;
}
else if (answer == "vas"){
cout << "\n Your current balance is: " << currentBalance << endl;
if (currentBalance > 0)
cout << "\nYour account is in good standing." << endl;
else{
cout << "You currently owe: $" << deficit << endl;
}
cout << "\nYour urrent balance is: $" << currentBalance << endl;
}
else if (answer == "Withdrawal"){
cout << "\nHow much would you like to take out? ";
cin >> withdrawal;
if (withdrawal > currentBalance)
cout << "\nYou don't have sufficient funds." << endl;
else{
updatedWithdrawal = (currentBalance - withdrawal);
cout << "You have $" << updatedWithdrawal << " left, cash is dispensed below." << endl;
cout << "\n\n\n Thank you, come again!" << endl;
}
}
else if (answer == "withdrawal"){
cout << "\nHow much would you like to take out? ";
cin >> withdrawal;
if (withdrawal > currentBalance)
cout << "\nYou don't have sufficient funds." << endl;
else{
updatedWithdrawal = (currentBalance - withdrawal);
cout << "\nYou have $" << updatedWithdrawal << " left, cash is dispensed below." << endl;
cout << "\n\n\n Thank you, come again!" << endl;
}
}
else if (answer == "Deposit"){
cout << "\nHow much would you like to deposit? ";
cin >> deposit;
updatedDeposit = (currentBalance + deposit);
cout << "\nYour previous balance of $" << prevBalance << " \nhas been updated and $" << deposit <<
" \nhas been added to your account, bringing the total available balance to $" << updatedDeposit << endl;
cout << "\n\nThank you come again!" << endl;
}
else if (answer == "deposit"){
cout << "\nHow much would you like to deposit? ";
cin >> deposit;
updatedDeposit = (currentBalance + deposit);
cout << "\nYour previous balance of $" << prevBalance << " \nhas been updated and $" << deposit <<
" \nhas been added to your account, bringing the total available balance to $" << updatedDeposit << endl;
cout << "\n\nThank you come again!" << endl;
}
else{
cout << "I don't recognize that command, restart and try again." << endl;
}
return 0;
}
Any help is greatly appreciated! :)
In order to use file in C++ you need an fstream. You should have a look to the documentation http://en.cppreference.com/w/cpp/io/basic_fstream/basic_fstream where is correctly described how to interact with file in C++.
You need an fstream opened in write mode in order to save all the data in the format that you've chosen. After that, you need to open an fstream in read mode in order to read from it the data again.
The file format is up to you. You can serialize data in whetever format you want. You need to remember that, after the serialization process, all the data must be read exactly how they are written.
This is my first answer to a question, but I just wanted to point out you know you can do else if (answer == "Withdrawal" || answer == "widthdrawal") the || is the or statement, it basically means that at least one of the conditions has to be true in order for the code in the brackets to execute. One thing about programming is make your code is DRY (Don't repeat yourself).
for the file: http://www.cplusplus.com/doc/tutorial/files/
So, anyways onto the file. you're going to want to read about it a bit, but the general process is
string line;
// File will contain a number.
ifstream myfile("balance.txt");
// if the file is open
if (myfile.is_open())
{
// get the first line of the file and store it to line (string)
getline(myfile, line);
// there is a better (foolproof) way to do this, but this will work for no.
currentBalance = atof(line.c_str());
// close file when done. always.
myfile.close();
}
You can use an std::ifstream to read, and an std::ofstream
to write. You're already familiar with these to some degree:
std::ifstream derives from std::istream, just as the type of
std::cin, does, and std::ofstream derives from
std::ostream, just as the type of std::cout does; all
of the functions which read and write are in the base class (or
depend on the base class).
The one particularity of the file streams is that you can open
them:
std::ifstream in( filename );
if ( ! in.is_open() ) {
// The open failed...
}
Same thing for the output stream.
It's not particularly easy to modify data in an existing file,
and the file needs to respect a lot of formatting rules to make
it even possible. For this reason, the most frequent way of
modifying files is to read them into memory, do the
modifications there, and then write the complete file back out.
Usually, you'll write the output to a temporary file, check that
the stream status is still good after the close, and then use
the functions remove and rename to remove the orginal file
and rename the new file. This way, if there is a problem
writing the file, you don't loose any data.
Related
Rewriting this question with a bit more knowledge on what I'm requesting; (Thank you James Risner and Turtle for your assistance, but I didn't word this correctly and got different responses than what was needed)
I am currently in the process of writing a program for my class in which I print out non-standard Unicode characters in a string. These characters are direct copies from a website, and not in u\ #### standard copy, but rather the unicode characters pre-selected. The program I am running this on is Clion, building my program using mingw's ninja build settings.
My issue that I'm experiencing is that my output, rather than the unicode characters, is instead a random array of (I think) unrelated characters. Printing this in Clion's Debug menu outputs the proper output, but printing it in the runtime or in its own external file all output the issue.
Below is an exact copy of my code (character for character) DO NOT REUSE PLEASE :(
#include <iostream>
#include <string>
#include <windows.h>
#define _WIN32_WINNT 0x0500
#include <thread>
#include <chrono>
#include <random>
using namespace std;
static int Range(int start, int end){
random_device rd;
mt19937 rng(rd());
uniform_int_distribution<int> dist(start,end);
return dist(rng);
}
int main() {
system("color 0F");
HWND consoleWindow = GetConsoleWindow();
int windowSize = 390;
MoveWindow(consoleWindow, windowSize,windowSize,windowSize,windowSize, TRUE); // This program and the one below it not only locks the window size, but also locks the window at a fixed display pixel length/width
SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);
ShowScrollBar(GetConsoleWindow(), SB_VERT, 0);
string name = "\033[91m▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀\n\033[92m███╗░░░███╗░█████╗░██████╗░███████╗███╗░░██╗██╗\n\033[93m████╗░████║██╔══██╗██╔══██╗██╔════╝████╗░██║██║\n\033[94m██╔████╔██║██║░░██║██║░░██║█████╗░░██╔██╗██║██║\n\033[95m██║╚██╔╝██║██║░░██║██║░░██║██╔══╝░░██║╚████║██║\n\033[96m██║░╚═╝░██║╚█████╔╝██████╔╝███████╗██║░╚███║██║\n\033[91m╚═╝░░░░░╚═╝░╚════╝░╚═════╝░╚══════╝╚═╝░░╚══╝╚═╝\n\033[92m▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀\n";
float balance = Range(1,50000);
float withD, cSelect;
int x = 0;
while (name[x] != '\0') {
cout << name[x] << flush;
this_thread::sleep_for(chrono::milliseconds(1));
x++;
}
Sleep(2);
std::cout << '\n' << endl;
string bottomBar = "\033[93m░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░█░\n";
string systName = "\033[94mModeni Systems LLC";
cout << " ";
cout << systName << "\n";
std::cout << '\n' << endl;
int y = 0;
while (bottomBar[y] != '\0') {
cout << name[y] << flush;
this_thread::sleep_for(chrono::milliseconds(1));
y++;
}
string Input;
cout << "\n \033[92mPlease Input your Name...\n\n ";
getline(cin, Input);
cout << endl;
cout << " Welcome " << Input << "!" << endl;
cout << " Please select a number from the options below!\n" << endl;
cout << bottomBar << endl;
cout << " 1.) View your Balance\n 2.) Make a withdrawal\n 3.) Deposit\n 4.) Customer Support\n 5.) Log Out Securely\n" << endl;
cout << bottomBar << endl;
cin >> cSelect;
while(cSelect != 5){
if(cSelect==1){
cout << "Your current balance is... " << balance << " dollars!\n \n" << endl;
cout << "Please select a number from the options given below!\n" << endl;
cout << bottomBar << endl;
cout << " 1.)View your Balance\n 2.)Make a withdrawal\n 3.)Deposit\n 4.)Customer Support\n 5.)Log Out Securely" << endl;
cin >> cSelect;
} else if(cSelect==2){
cout << "Please enter the amount you'd like to withdraw!\n" << endl;
cin >> withD;
if(withD>balance){
cout << "Sorry, you do not have that much money! Please try again... " << endl;
} else if(withD<=balance) {
balance = balance - withD;
cout << "Successfully taken out " << withD << " dollars!\n" << "Your new balance is " << balance << " dollars!" << endl;
cout << "Please select a number from the options given below!\n" << endl;
cout << bottomBar << endl;
cout << " 1.)View your Balance\n 2.)Make a withdrawal\n 3.)Deposit\n 4.)Customer Support\n 5.)Log Out Securely" << endl;
cin >> cSelect;
}
} else if(cSelect==3){
cout << "Please enter the amount you'd like to deposit!" << endl;
double depAm;
cin >> depAm;
balance = balance + depAm;
cout << "Your new balance is now " << balance << " dollars!" << endl;
cout << "Please select a number from the options given below!\n" << endl;
cout << bottomBar << endl;
cout << "1.)View your Balance\n2.)Make a withdrawal\n3.)Deposit\n4.)Customer Support\n5.)Log Out Securely" << endl;
cin >> cSelect;
} else if(cSelect==4){
cout << bottomBar << "\n" << endl;
cout << "Hello! This is Modeni's Self-Service Assistant!\n Please describe your problem below! \n" << endl;
string proB;
cin >> proB;
int chatF;
chatF = Range(0,5);
if(chatF==0){
cout << "We're so sorry to hear that! Please wait as we get you in touch with someone who can help.\n" << endl;
} else if(chatF==1){
cout << "Sorry to hear that you're currently having that problem! Please sit tight as we get you in touch with someone who can help.\n" << endl;
} else if(chatF==2){
cout << "That's not good! Please wait just a moment as we get you in touch with someone who can help.\n" << endl;
} else {
cout << "Ouch! Just give us a moment while we put you in touch with someone who can help!\n" << endl;
}
Sleep(10000);
string nameSt;
int nameVar;
nameVar = Range(0,5);
if(nameVar == 1)
nameSt = "Raphael";
else if(nameVar == 2)
nameSt = "Marie";
else if(nameVar == 3)
nameSt = "Joesph";
else
nameSt = "Marian";
cout << " \033[94m░█░█ " << nameSt << " has joined the chat █░█░" << endl;
Sleep(Range(3000,8000));
cout <<"\n >> Just a moment while I look over your concern.\n" << endl;
Sleep(Range(3000,8000));
cout << " >> Alright. I'm sorry you're dealing with this problem right now. Let's put you in touch with one of our call-in agents to assist you further.\n" << endl;
Sleep(Range(3000,8000));
cout << " >> Their number is - 1-(918)-335-1300.\n" << endl;
Sleep(Range(3000,8000));
cout << " >> Is there anything else I can help you with today? \n" << endl;
return 0;
}
}
return 0;
}
I am specifically writing this program for windows, and I have yet to find a solid fix without re-writing the entirety of my code.
The intended output is photograph 1, and the actual output is photograph 2. Any and all help is appreciated!
Photograph 1
Photograph 2
There is no problem with your program. There is a problem with your display.
% cc -o modeni modeni.cpp -lc++
% ./modeni
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
███╗░░░███╗░█████╗░██████╗░███████╗███╗░░██╗██╗
████╗░████║██╔══██╗██╔══██╗██╔════╝████╗░██║██║
██╔████╔██║██║░░██║██║░░██║█████╗░░██╔██╗██║██║
██║╚██╔╝██║██║░░██║██║░░██║██╔══╝░░██║╚████║██║
██║░╚═╝░██║╚█████╔╝██████╔╝███████╗██║░╚███║██║
╚═╝░░░░░╚═╝░╚════╝░╚═════╝░╚══════╝╚═╝░░╚══╝╚═╝
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
% echo $TERM
xterm-256color
I am using a macOS system and this works with TERM=xterm-256color on both iTerm2 and Terminal.
What is TERM?
This environment variable advises applications what terminal emulation is required to display characters on screen properly. An application will use the termcap/terminfo database to look up the proper escape sequences to display colors, move or manipulative text on screen, and other effects.
I only ever see xterm-256color now. Why?
Modern terminal applications assume the output will be in xterm-256color format. Many no longer have an option to choose another format.
AFAIK there is no standard way in C++ to process unicode in standard io, and different OSs' default consoles will behave differently if you just make the unicode string as std::string to output; E.g. in Windows cmd maybe you need _setmode(_fileno(stdout), _O_U16TEXT); to show UTF-16 strings correctly.
Good news is that in C++23 there will be <print> to (hopefully, not definitely so far) help solve this troublesome problem.
I tried running my code. no error but there's is no output in the data file itself when I reinterpret cast.
May I know what is missing?
Thank you.
I need the dat file to take in all the input provided by the cin
Use securing confined his shutters. Delightful as he it acceptance an solicitude discretion reasonably. Carriage we husbands advanced an perceive greatest. Totally dearest expense on demesne ye he. Curiosity excellent commanded in me. Unpleasing impression themselves to at assistance acceptance my or. On consider laughter civility offended oh.
Spot of come to ever hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behaviour. Him yet devonshire celebrated especially. Unfeeling one provision are smallness resembled repulsive.
In alteration insipidity impression by travelling reasonable up motionless. Of regard warmth by unable sudden garden ladies. No kept hung am size spot no. Likewise led and dissuade rejoiced welcomed husbands boy. Do listening on he suspected resembled. Water would still if to. Position boy required law moderate was may.
struct task
{
char title[MAX]; // Eg. Assignment ,Exam,Test
int weight; // Weightage of the task
int fullmark; // Upon
float mark; // Obtained marks
};
struct Subject
{
char subCode[MAX]; // CSCI103 MATH STAT
char subTitle[MAX]; // Full title of subject
int noTask; // No. of task for following struct
task Task[MAX]; // Array of tasks
float finalMark; // Final overall mark for subject
Grade finalGrade; // Grade for subject
};
int main()
{
fstream afile;
afile.open ("test.dat", ios::in | ios::binary | ios::app);
int totalWeight = 0;
Subject S;
if(!afile)
{
cout << "Error opening file,please check" << endl;
exit(1);
}
cout << "------------------" << endl
<< "Subject adding system" << endl
<< "------------------" << endl << endl;
cout << "Subject Code: ";
cin >> S.subCode;
cin.clear();
cin.ignore(100,'\n');
cout << "Subject Name: ";
cin.getline (S.subTitle, MAX);
cout << "No of assessment tasks: ";
cin >> S.noTask;
cin.clear();
cin.ignore(100,'\n');
cout << endl;
// Loop for binary file
for(int i = 1;i<=S.noTask;i++)
{
cout << "Task " << i << " Information" << endl
<< "\t Title: ";
cin >> S.Task[i].title;
cin.clear();
cin.ignore(100,'\n');
cout << "\t Weight: ";
cin >> S.Task[i].weight;
cin.clear();
cin.ignore(100,'\n');
cout << "\t Upon: ";
cin >> S.Task[i].fullmark;
cin.clear();
cin.ignore(100,'\n');
totalWeight +=S.Task[i].weight;
}
cout << endl << "Subject " << S.subTitle << " added to system" << endl
<< "Total weight = " << totalWeight << endl;
afile.write (reinterpret_cast <const char*>(&S), sizeof (S));
afile.close();
}
}
You open the file with the flag std::ios::in (therefore you open it for reading), and try to write to it with afile.write(&S, sizeof(S))
If you want to write to the file you can either:
Open it with std::ios::out insteand of std::ios::in
Use an std::ofstream instead of an std::fstream and omit the std::ios::in/out flag
Also, in the future, try to reduce the code in question to the minimum to understand you problem.
I have been given and assignment to read a file of data listed below:
Turn on the Bright Lights
Interpol
9.49
House of Jealous Lovers
The Rapture
1.29
Fever to Tell
Yeah Yeah Yeahs
6.99
Desperate Youth, Blood Thirsty Babes
TV on the Radio
8.91
The Fragile
Nine Inch Nails
12.49
Input this data into C++ and then display in a matrix and output total price of the cd's and how many cd's were sold. So far I can only get the first line to display properly and cannot figure out what I need to change to get the rest to display. Here is what I have written thus far. I have not started on the output code and feel as though I will not have an issue with that.
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
ifstream inputFile, processLine;
ofstream outputFile;
string title, band;
double price, total = 0;
inputFile.open("orders.txt");
while (!inputFile.eof())
{
getline(inputFile, title);
getline(inputFile, band);
inputFile >> price;
total += price;
cout << "Welcome to Megan McCracken's Online Music Store" << endl;
cout << "You have submitted the following order:" << endl;
cout << "***************************************************************************" << endl;
cout << "Title" << setw(46) << "Artist" << setw(24) << "Cost" << endl;
cout << title << setw(28) << band << setw(22) << fixed << setprecision(2) << price << endl;
cout << title << setw(30) << band<< setw(19) << fixed << setprecision(2) << price << endl;
cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
cout << "--------------------------------------------------------------------------" << endl;
cout << "Total Due:" << setw(75) << fixed << setprecision(2) << total << endl;
cout << "==========================================================================" << endl;
}
/* getline(inputFile, title);
cout << left << title;
getline(inputFile, band);
cout << setw(23) << band;
inputFile >> price;
cout << setw(10) << fixed << setprecision(2) << price << endl; */
system("pause");
return 0;
}
There are multiple problems with this code.
while (!inputFile.eof())
This is always a bug. See the linked article for more information.
But this is not the only problem.
getline(inputFile, band);
inputFile >> price;
Do not use both std::getline() and operator>> on the same input stream. operator>> has certain, narrowly-constrained, semantics when it comes to handling whitespace. Here, operator>> is not going to consume the trailing newline, so on the next loop iteration the first getline() is going to go off the rails.
Although there are some band-aid fixes that are often suggested, in this case, the simplest solution is to simply not use operator>>.
Use getline() a third time, to read the third line into a std::string. Then using it to construct an independent std::istringstream, and use operator>> with that. Problem solved.
After fixing that, there are some logical errors that need to be fixed. The header of the report should likely be displayed before the loop, and inside the loop the only thing that should be done is displaying a single line of the receipt, and adding up the totals, then displaying the total after the end of file was received.
How can I properly use a do, while, or for loop of some kind to prevent the user from entering anything else other than the answer "1" or "2"?
If they don't, the program should tell them they cannot do that and then return them to the previous question.
#include <iostream>
#include "Options.h"
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
cout << "\tWelcome to my text adventure game\n" << endl;
cout << "Please enter your characters Name: ";
string name;
cin >> name;
cout << "\nWelcome " << name << endl;
cout << "\nPlease choose the side you would like to play in - ";
Options OptionsObject;
OptionsObject.optionsSide();
string answer;
while(answer != "quit") {
cin >> answer;
cout << answer << endl;
}
if ( answer == "1" ) {
cout << "You chose the good side. Let the game begin\n " << endl;
cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl;
cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl;
cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is " << endl;
} else if( answer == "2" ) {
cout << "hey there" << endl;
}
}
Seen that this problem would be in all the choose the user will do during the game I will make an external function, to let the user choose. Maybe a static class working in this way.
int ans = Answer.getUserAnswer(2,"[1/2]>");
and this should work in this way:
public static int getUserAnswer(int max =1,string message="[1/1]>")
{
int ans = 0;
while(ans==0){
cout<<message<<" ";
cin >> answer;
if(answer>0 and answer<=max) return ans;
cout<<"\tnot a valid choose\n";
ans=0;
}
}
and you will have in your ans one of the value you expect, and it will print "not a valid choose" if the answer is not between the one you expect, and you can do this even with 10 answers, calling it by default expect you to give him just the number one.
I used it in a console game I made ;)
Hope is useful
EDIT
int getUserAnswer(int max =1,string message="[1/1]>")
{
int ans = 0;
while(ans==0){
cout<<message<<" ";
cin >> answer;
if(answer>0 and answer<=max) return ans;
cout<<"\tnot a valid choose\n";
ans=0;
}
}
and to request this code in your code:
cout << "\nWelcome " << name << endl;
cout << "\nPlease choose the side you would like to play in - ";
Options OptionsObject;
OptionsObject.optionsSide();
int answer =getUserAnswer(2,"[1/2]>");
if (answer == 1){
cout << "You chose the good side. Let the game begin\n " << endl; cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl; cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl; cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is " << endl;
}
else if(answer == 2){
cout << "hey there" << endl;
}
You should put the line where you read the input in a do loop that loops while the answer given by the user is invalid.
So, your do loop should be:
string answer;
cin >> answer;
// loop while answer is neither "1" nor "2"
while( answer != "1" && answer != "2" ) {
cout << "Please enter a 1 or a 2. You entered " << answer << endl;
cin >> answer;
}
I need to take in the make model and years of the car the particular product fits. The program is going to write a file with text in it that will be an outline of the html. I need with the crucial information inserted into the correct spots to quickly be able to add products to our web page. When I use a switch statement or if statements to separate the categories of the products, the input gets messed up and does not let me answer one of the questions. I can't just use cin because I need to take dates that look like "2008 - 2009 - 2010 - 2011".
Here is what I've got so far! This one is just regular cin, I have tried getline you could quickly c I started yesterday and I'm pretty new so forgive me if this is an easy fix, but I cant find anything.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void proinfo();
int main()
{
//Sytem Information
system("TITLE This is a Beta of My Product Information Template Version 0.0.2");
//I'm using this as a welcome screen to inform users of new improvements on patches and other pertinent information to the users
cout << "Welcome To My Product Information Template Maker!!! \n It Is Still In Development" << endl;
cout << "\n\nworking features are:" << endl << "\t-None\n" << endl;
system("PAUSE");
system("CLS");
proinfo();
}
void proinfo()
{
//Declare Variables here
int loop(1), protype;
char make[256], model[256], year[256];
string getinput;
while(loop==1)
{
//This is the first menu the user sees where they have to choose what type
//of products users will be adding
system("CLS");
cout << "Welcome to My Product Information Template Version 0.0.0" << endl;
cout << "Please select the number of the product" << endl;
cout << "type you will be ading!\n" << endl;
cout << "1.Fe - Fe2 Moldings" << endl;
cout << "2.CF - CF2 Moldings" << endl;
cout << "What would you like to do? ";
cin >> protype;
if(protype==1)
{
//FE - FE2 Molding
system("cls");
cout << "You have selected" << endl;
cout << "Fe - Fe2 Moldings\n" << endl;
cout << "Please Enter the Make of the molding" << endl;
cin >> make;
cout << "Please Enter the Model of the molding" << endl;
cin >> model;
cout << "Please Enter the Years this molding fits" << endl;
cin >> year;
cout << "You have just created a template for a(n)" << year << " " << make << " " << model << endl;
cout << "Check My Documents For a file called Fe-Fe2template.txt" << endl;
//Asks to quit or restart
cout << "Would you like to make another tempalte?" << endl;
cin >> getinput;
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
system("PAUSE");
}
if(protype==2)
{
//CF - CF2 Molding
system("cls");
//Asks to quit or restart
cout << "Would you like to make another tempalte?" << endl;
cin >> getinput;
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
system("PAUSE");
//End of Protype Switch Statement
}
//End of while loop
}
//End of Proinfo()
}
Change year[256] to string year;
Change cin >> year; to getline(cin, year);
Add the line cin.ignore(); before the getline.
Your main problem is that the stream operator >> leaves the newline in the stream so when you try to use getline it reads an empty line. ignore() will chew up the newline and let you read the string you expect.
So this should get you on your way.
cin.ignore();
cout << "Please Enter the Years this molding fits" << endl;
getline(cin, year);
You have some other small problems but you'll figure them out. The worst is forgetting to terminate the loop.
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
else
{
loop = 0;
continue;
}