I have a big problem...
Just run the program and go to "New Gun", then "Handgun".
The problem comes when you have to enter the model.
For example, if i enter "Desert Eagle", in the text file it outputs only "Eagle". Its strange, and i cant solve it.
code:
#include <fstream>
#include <iostream>
#include <windows.h>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
void setcolor(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
int main (){
system("chcp 1251 > nul");
system("title GunnZone");
string gunModel;
int gunManufactureYear;
int itemNumber;
double gunWeight;
double price;
ofstream myfileHandguns("Handguns.txt", ios::app);
ofstream myfileRifles("Rifles.txt", ios::app);
ofstream myfileShotguns("Shotguns.txt", ios::app);
ofstream myfileSnipers("Snipers.txt", ios::app);
ofstream myfileGranades("Granades.txt", ios::app);
ofstream myfileExplosives("Explosives.txt", ios::app);
int choice;
cout << "1.New Gun" << endl;
cout << "2.List Guns" << endl;
cout << "3.Exit" << endl << endl;
cout << "Enter your choice: ";
cin >> choice;
if(choice == 1){
system("cls");
int gunTypeChoice;
cout << "1.Handgun" << endl;
cout << "2.Rifle" << endl;
cout << "3.Shotgun" << endl;
cout << "4.Sniper" << endl;
cout << "5.Granade" << endl;
cout << "6.Explosives" << endl << endl;
cout << "Enter your choice: ";
cin >> gunTypeChoice;
if(gunTypeChoice == 1){
system("cls");
cout << "Model: ";
cin >> gunModel;
getline(cin, gunModel);
cout << endl << endl;
cout << "Year of Manufacture: ";
cin >> gunManufactureYear;
cout << endl << endl;
cout << "Weight: ";
cin >> gunWeight;
cout << endl << endl;
cout << "Item Number: ";
cin >> itemNumber;
cout << endl << endl;
cout << "Price: ";
cin >> price;
myfileHandguns << "Model: " << gunModel << "\n\n";
myfileHandguns << "Year of Manufacture: " << gunManufactureYear << "\n\n";
myfileHandguns << "Weight: " << gunWeight << " g" << "\n\n";
myfileHandguns << "Item Number: " << itemNumber << "\n\n";
myfileHandguns << "Price: " << price << "$" << "\n\n";
myfileHandguns.close();
}
}
system("pause > nul");
return 0;
}
Remove >> operator as suggested. Use clear and ignore to clear errors skip new line characters \n
cout << "Model: ";
//cin >> gunModel;
std::cin.clear();
std::cin.ignore(0xffff, '\n');
std::getline(cin, gunModel);
cout << "Testing... you entered " << gunModel << endl;
cout << endl << endl;
See also Why would we call cin.clear() and cin.ignore() after reading input?
Related
I'm currently making a record system for ticket booking as an assignment and facing problems when I want the data to read after modifying my file. I saw there were 2 options for me: eof or read in fstream.
The code for my modify records:
ifstream inFile;
ofstream outFile;
inFile.open("movieRecord.dat");
outFile.open("temp.dat");
inFile >> item.movieName >> item.movieTime >> item.hallNum >> item.moviePrice;
cout << "\tPlease Enter the Movie to Edit:";
cin.ignore();
cin.getline(editMovie,20);
titleMatch = strcmp (editMovie,item.movieName);
while (!inFile.eof()){
if (editMovie == item.movieName){
break;
}
else{
outFile << item.movieName << " " << item.movieTime << " " << item.hallNum << " " << item.moviePrice << endl;
break;
}
}
cout << "\tEditing " << editMovie << "..." << endl;
cin.clear();
cout << "\tReInsert Movie Title: ";
cin.getline(movieTitle,20);
cin.clear();
cout << flush;
cout << "\tEnter Time(ie:15.00): ";
cin >> movieTime2;
cout << flush;
cout << "\tEnter Hall Number for Movie: Hall ";
cin >> hallNumber;
cout << flush;
cout << "\tEnter Price of Movie Ticket: RM";
cin >> ticketPrice;
outFile << movieTitle << " " << movieTime2 << " " << hallNumber << " " << ticketPrice;
outFile.close();
inFile.close();
remove ("movieRecord.dat");
rename ("temp.dat","movieRecord.dat");
cout << "\tEdit Saved!" << endl;
system("pause");
goto adminMenu;
}
My code for reading the file:
ifstream movieCheck;
movieCheck.open("movieRecord.dat",ios::in);
movieCheck >> item.movieName >> item.movieTime >> item.hallNum >> item.moviePrice;
if (!movieCheck.good()){
cout << "File Open Error!" << endl;
}
while (movieCheck.read((char*)&item,sizeof(item))){
cout << "\tMovie title: " << item.movieName << endl;
cout << fixed;
cout << "\tTime: " << setprecision(2) << item.movieTime << endl;
cout << "\tHall Number: Hall " << item.hallNum << endl;
cout << "\tMovie Ticket Price: RM " << setprecision(2) << item.moviePrice << endl;
cout << "\t==============================================" << endl;
movieCheck >> item.movieName >> item.movieTime >> item.hallNum >> item.moviePrice;
}
movieCheck.close();
cout << endl << "\t The End of the Movie Summaries!" << endl;
When I use eof, the output will only show the first data only, simple example:
Desired Output:
Hello
Help2 \The edited record
Current Output:
Hello \does not show the edited record
When using read function however, my output is either similar to eof or it would not show at all.
I have a problem in outputting my code to a text file. I just wanted to print a title at the beginning of a .txt file, but it happened to end up printing along with new lines of data. Basically, I am appending new data to the text file using ios::app. How can I achieve not printing my title in the text file all over again after appending other data?
Text File Output
Here is my code btw...
#include <iostream>
#include <fstream>
#include <string>
void QuaranDiary();
struct quarantine3 //structure for QuaranDiary
{
string QuaranDiaryMonth;
string QuaranDiaryDay;
string QuaranDiaryYear;
string QuaranDiaryHour;
string QuaranDiaryMinute;
string QuaranDiaryUpdate;
};
quarantine3 diary[500];
//QuaranDiary
void QuaranDiary()
{
int i = 0, update = 0;
char QuaranDiaryMore;
do
{
cout << "=====================================================================================" << endl;
cout << " COVID-19 QUARANDIARY " << endl;
cout << "=====================================================================================" << endl << endl;
cout << "DATE (MM/DD/YYYY)" << endl << endl;
cout << "Month : ";
cin >> diary[i].QuaranDiaryMonth;
cout << "Day : ";
cin >> diary[i].QuaranDiaryDay;
cout << "Year : ";
cin >> diary[i].QuaranDiaryYear;
cout << endl;
cout << "[" << diary[i].QuaranDiaryMonth << "/" << diary[i].QuaranDiaryDay << "/" << diary[i].QuaranDiaryYear << "]" << endl << endl;
cout << "TIME (23:59)"<<endl<<endl;
cout << "Hour : ";
cin >> diary[i].QuaranDiaryHour;
cout << "Minute : ";
cin >> diary[i].QuaranDiaryMinute;
cout << endl;
cout << "[" << diary[i].QuaranDiaryHour << ":" << diary[i].QuaranDiaryMinute << "]" << endl << endl;
cout << "Health Update: ";
cin.ignore();
getline(cin, diary[i].QuaranDiaryUpdate);
update++;
cout << "\n\nAdd More to QuaranDiary? [Y/N]: ";
cin >> QuaranDiaryMore;
QuaranDiaryMore = toupper(QuaranDiaryMore);
while (toupper(QuaranDiaryMore) != 'Y' && toupper(QuaranDiaryMore) != 'N')
{
cout << "\nWrong Input. Try Again!\n" << endl;
cin.clear();
cin.ignore();
cout << "Add More to QuaranDiary? [Y/N]: ";
cin >> QuaranDiaryMore;
}
cin.clear();
cin.ignore();
system("CLS");
//fstream for QuaranDiary
ofstream QuaranDiary;
QuaranDiary.open("QuaranDiary.txt", ios::app);
QuaranDiary << "=====================================================================================" << endl;
QuaranDiary << " COVID-19 QUARANDIARY " << endl;
QuaranDiary << "=====================================================================================" << endl;;
QuaranDiary << "\nDate: " << diary[i].QuaranDiaryMonth << "/" << diary[i].QuaranDiaryDay << "/" << diary[i].QuaranDiaryYear;
QuaranDiary << "\tTime: " << diary[i].QuaranDiaryHour << ":" << diary[i].QuaranDiaryMinute << endl << endl;
QuaranDiary << "[Health Update]\n\n" << diary[i].QuaranDiaryUpdate << endl;
QuaranDiary << "=====================================================================================" << endl;
if (QuaranDiary.is_open())
{
cout << "\nCheck QuaranDiary.txt for your written health updates!" << endl;
}
else
{
cout << "\n\nUnable to Open File!" << endl;
}
QuaranDiary.close();
} while (toupper(QuaranDiaryMore == 'Y'));
}
I am trying to make a program that will take a users input to make multiple forms. I am stuck on trying to get the vector (that will be filled with objects of the form class that the user creates) to be use-able in other functions. When I use the address-of operator (&) it gives me this error when the program gets to letting the user input the data to the objects.
This is the screen capture of the program and the error.
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Form {
public:
string Fname;
string Lname;
string City;
string Street;
string State;
string ZipCode;
};
void menuMain();
void menu1st(vector<Form> &Fvect);
void menu1st(vector<Form> &Fvect)
{
int MainM;
int n;
cout << "NEW FORM(s)" << endl;
cout << "Enter the number of forms you would like to make (Maximum of 5): "; cin >> n; cout << endl;
for (int i = 0; i < n; i++)
{
cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
cout << "City: "; cin >> Fvect[i].City; cout << endl;
cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
cout << "State: "; cin >> Fvect[i].State; cout << endl;
cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
}
cout << "Enter 1 to go back to main: "; cin >> MainM;
if (MainM == 1)
{
menuMain();
}
else
{
cout << "Error not a correct input." << endl;
}
}
void menu2nd()
{
int MainM;
//int Fnum;
vector<Form> Fvect;
cout << "EDIT A FORM" << endl;
cout << Fvect[1].Fname;
cout << "Enter the ";
cout << "Enter 1 to go back to main: "; cin >> MainM;
if (MainM == 1)
{
menuMain();
}
else
{
cout << "Error not a correct input." << endl;
}
}
void menuMain()
{
int Pnum;
cout << "INFORMATION FORMATTING PROGRAM" << endl;
cout << "1. Create new form's." << endl;
cout << "2. Edit a form." << endl;
cout << "3. Print forms." << endl;
cout << "4. Erase a form." << endl;
cout << "5. Exit Program." << endl;
cout << "Enter the action you want to take (1-5): "; cin >> Pnum;
vector<Form> Fvect;
if (Pnum == 1)
{
menu1st(Fvect);
}
if (Pnum == 2)
{
menu2nd();
}
else
{
cout << "Error not a correct input." << endl;
}
}
int main()
{
menuMain();
}
You are accessing Fvect using an invalid index in the following lines:
cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
cout << "City: "; cin >> Fvect[i].City; cout << endl;
cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
cout << "State: "; cin >> Fvect[i].State; cout << endl;
cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
Consequently, your program has undefined behavior.
You need to have items in a std::vector before you can access an item from it using the array syntax. What you need to do is:
Read the data to an object of type Form.
Add the object to the std::vector.
Replace those lines with:
Form form;
cout << "First Name: "; cin >> form.Fname; cout << endl;
cout << "Last Name: "; cin >> form.Lname; cout << endl;
cout << "City: "; cin >> form.City; cout << endl;
cout << "Street: "; cin >> form.Street; cout << endl;
cout << "State: "; cin >> form.State; cout << endl;
cout << "Zip Code: "; cin >> form.ZipCode; cout << endl;
Fvect.push_back(form);
PS
I am not sure why you have the cout << endl; in those lines. You don't need them. It will be sufficient to use:
cout << "First Name: "; cin >> form.Fname;
cout << "Last Name: "; cin >> form.Lname;
cout << "City: "; cin >> form.City;
cout << "Street: "; cin >> form.Street;
cout << "State: "; cin >> form.State;
cout << "Zip Code: "; cin >> form.ZipCode;
i'm trying to load the file into an array when the program starts so i can
modify or search in it i don't know if my code works or not ( it's not reading the file )i have the file
and there's two books in
i have tried to debug it but couldn't find the problem the code works
but it think there's a problem with the load() function i don't know what
my code :
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct books{
//identfying books with all needed things
int id, status;
string title, p_name, p_address;
string date;
string aut_name, aut_nationality;
}newbook[10000], aut[10000];
int i = 0;
void load(){
ifstream myfile("books.txt", ios::in);
while (myfile >> newbook[i].id >> newbook[i].title >> newbook[i].p_name >> newbook[i].p_address >> aut[i].aut_name >> aut[i].aut_nationality >> newbook[i].date >> newbook[i].status)
i++;
}
void search_for_book(){
int temp = 0;
int idx;
cout << "enter the ID of the book you're looking for : ";
cin >> idx;
for (int srh = 0; srh < i; srh++){
if (newbook[srh].id == idx){
cout << setw(10) << "book found :" << endl;
cout << "title :" << newbook[srh].title << endl;
cout << "publisher name : " << newbook[srh].p_name << endl;
cout << "publisher address" << newbook[srh].p_address << endl;
cout << "author name :" << aut[srh].aut_name << endl;
cout << "author Nationality :" << aut[srh].aut_nationality << endl;
cout << "publish Date :" << newbook[srh].date << endl;
cout << "status :" << newbook[srh].status << endl;
temp++;
break;
}
else
srh++;
}
if (temp == 0){
cout << "couldn't find book" << endl << endl;
}
}
int main(){
load();
char choice;
cout << "enter your choice (3)";
cin >> choice;
if (choice == '3'){
search_for_book();
}
}
note :*( there are other functions like adding new book but not necessary to write )
*(i'm new to c++ don't really know how to read file into memory but i'm trying)
this is the code to save data into file :
void add_new_book_5(){
int booksnumber;
books newbook[1000], aut[100];
cout << "how many books you want to add ? ";
cin >> booksnumber;
cout << "what books you want to add :" << endl;
d_base.open(path, ios::out | ios::app);
for (int i = 0; i < booksnumber; i++){
cout << "id please : "; cin >> newbook[i].id;
cout << "title : "; cin.ignore(); getline(cin, newbook[i].title);
cout << "publisher name :"; getline(cin, newbook[i].p_name);
cout << "publisher address : "; getline(cin, newbook[i].p_address);
cout << "author" << " name : "; cin.ignore(); getline(cin, newbook[i].aut_name);
cout << "Nationality : "; getline(cin, newbook[i].aut_nationality);
cout << "Publish date :"; getline(cin, newbook[i].date);
cout << "How many copies of " << newbook[i].title << " "; cin >> newbook[i].status;
system("cls");
d_base << newbook[i].id << "\ " << newbook[i].title << "\ ";
d_base << newbook[i].p_name << "\ " << newbook[i].p_address << "\ ";
d_base << newbook[i].aut_name << "\ " << newbook[i].aut_nationality << "\ ";
d_base << newbook[i].date << "\ " << newbook[i].status << endl;
}
d_base.close();
cout << setw(76) << "Books Have Been Saved Sucessfully" << endl;
}
I have this code where the string info is not working as it should.
I have a getline where info is inside and it saves a hole sentence, but it won't output more than the first word.
Example:
"Hello world!"
I will only show me "hello"
Here is the part of the code:
void add() {
string name;
string faction;
string classe;
string race;
string info;
ofstream wowdatabase("wowdatabase.txt", ios::app);
cout << "Add a race or class" << endl;
cout << "---------------------------------------------------------" << endl;
cout << "" << endl;
cout << "Enter the name of the race or class (only small letters!):" << endl;
cin >> name;
cout << "Enter Race (Type -, if writen in name section):" << endl;
cin >> race;
cout << "Enter Class (Type -, if writen in name section):" << endl;
cin >> classe;
cout << "Enter faction (Alliance/Horde):" << endl;
cin >> faction;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cout << "Enter the information:" << endl;
getline(cin, info);
cout << info;
system("pause");
wowdatabase << name << ' ' << faction << ' ' << classe << ' ' << race << ' ' << info << endl;
wowdatabase.close();
system("CLS");
main();
}
void searchname() {
ifstream charecter("wowdatabase.txt");
string name;
string find;
string faction;
string classe;
string race;
string info;
int i = 0;
system("CLS");
cout << "Search for a race or class" << endl;
cout << "---------------------------------------------------------" << endl;
cout << "" << endl;
cout << "Please enter name:";
cin >> find;
while (charecter >> name >> faction >> classe >> race >> info ) {
if (find == name) {
system("CLS");
cout << "Charecter found" << endl;
cout << "---------------------------------------------------------" << endl;
cout << "" << endl;
cout << "Name of race/class: " << name << endl;
cout << "Race: " << race << endl;
cout << "Class: " << classe << endl;
cout << "Faction: " << faction << endl;
cout << "Info: " << info << endl;
cout << "" << endl;
cout << "Press enter to return to menu" << endl;
system("pause");
system("CLS");
main();
}
}
You use variable info in several functions. For example in function searchname() you enter it as
while (charecter >> name >> faction >> classe >> race >> info )
that is in this function you do not use getline. Also your code is invalid because you may not recursively call main in C++.