I am able to enter the first student, however, once I request the second student's input it does not accept the: getline(cin, s2) on to getline(cin,s10).
Could you please help me understand where the mistake is made or why the output is not following the same format as s1/GPA1?
Here is my code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Welcome
cout << "Welcome to your personal GPA Calculator! \n" << endl;
float GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7, GPA8, GPA9, GPA10 = 0;
string s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
//Requesting User Input
cout << "Student One| Please enter your Last Name, First Name: " << endl;
getline(cin, s1);
cout << "What is your current GPA: " << endl;
cin >> GPA1;
cout << endl;
cout << "Student Two| Please enter your Last Name, First Name: " << endl;
getline(cin, s2);
cout << "What is your current GPA: " << endl;
cin >> GPA2;
cout << endl;
cout << "Student Three| Please enter your Last Name, First Name: " << endl;
getline(cin, s3);
cout << "What is your current GPA: " << endl;
cin >> GPA3;
cout << endl;
cout << "Student Four| Please enter your Last Name, First Name: " << endl;
getline(cin, s4);
cout << "What is your current GPA: " << endl;
cin >> GPA4;
cout << endl;
cout << "Student Five| Please enter your Last Name, First: " << endl;
getline(cin, s5);
cout << "What is your current GPA: " << endl;
cin >> GPA5;
cout << endl;
cout << "Student Six| Please enter your Last Name, First Name: " << endl;
getline(cin, s6);
cout << "What is your current GPA: " << endl;
cin >> GPA6;
cout << endl;
cout << "Student Seven| Please enter your Last Name, First Name: " << endl;
getline(cin, s7);
cout << "What is your current GPA: " << endl;
cin >> GPA7;
cout << endl;
cout << "Student Eight| Please enter your Last Name, First Name: " << endl;
getline(cin, s8);
cout << "What is your current GPA: " << endl;
cin >> GPA8;
cout << endl;
cout << "Student Nine| Please enter your Last Name, First Name: " << endl;
getline(cin, s9);
cout << "What is your current GPA: " << endl;
cin >> GPA9;
cout << endl;
cout << "Student Ten| Please enter your Last Name, First Name: " << endl;
getline(cin, s10);
cout << "What is your current GPA: " << endl;
cin >> GPA10;
cout << endl;
//Store in Tabular Format
cout << ("Student Name| \t\t |Student GPA Value \n");
cout << ("____________________________________________\n");
std::cout << s1 << "\t\t " << std::setprecision(2) << std::fixed << GPA1 << endl;
std::cout << s2 << "\t\t " << std::setprecision(2) << std::fixed << GPA2 << endl;
std::cout << s3 << "\t\t " << std::setprecision(2) << std::fixed << GPA3 << endl;
std::cout << s4 << "\t\t " << std::setprecision(2) << std::fixed << GPA4 << endl;
std::cout << s5 << "\t\t " << std::setprecision(2) << std::fixed << GPA5 << endl;
std::cout << s6 << "\t\t " << std::setprecision(2) << std::fixed << GPA6 << endl;
std::cout << s7 << "\t\t " << std::setprecision(2) << std::fixed << GPA7 << endl;
std::cout << s8 << "\t\t " << std::setprecision(2) << std::fixed << GPA8 << endl;
std::cout << s9 << "\t\t " << std::setprecision(2) << std::fixed << GPA9 << endl;
std::cout << s10 << "\t\t " << std::setprecision(2) << std::fixed << GPA10 << endl;
return (0);
}
You generally don't want to read numbers directly with cin for interactive use. It doesn't play well with newlines -- leaving the newline for the next entry. In this case, you get a blank name for person 2.
Try this:
string temp;
//Requesting User Input
cout << "Student One| Please enter your Last Name, First Name: " << endl;
getline(cin, s1);
cout << "What is your current GPA: " << endl;
getline(cin, temp);
GPA1 = strtof(temp.c_str(), 0);
cout << endl;
You have to be careful when you mix cin and getline(cin, "string"). Cin will ignore any empty spaces or lines while getline will not. What's happening is that after you input the first student's GPA, a newline character is stored in the buffer and is carried over to getline where it's entered automatically. Try using cin.ignore() after every cin >> gpa.
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'm currently working on an exercise given to me by my university professor and one of the questions is to create a program using data structures which inputs data of any topic and outputs them in a neat format. The problem is the question specifically states to not use arrays, and shorter codes grants bonus marks. How should I shorten my code below?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct musicRecord //structure name
{
//structure members
string title;
string artist;
string album;
string genre;
string releaseYear;
};
int main()
{
int numRecords;
musicRecord music1, music2, music3, music4, music5; //structure variables
//input
cout << "===================================================";
cout << "\nRecord No. 1"<< endl;
cout << "\nEnter the title : ";
getline(cin, music1.title);
cout << "\nEnter the artist's name : ";
getline(cin, music1.artist);
cout << "\nEnter the album name : ";
getline(cin, music1.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music1.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music1.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 2" << endl;
cout << "\nEnter the title : ";
getline(cin, music2.title);
cout << "\nEnter the artist's name : ";
getline(cin, music2.artist);
cout << "\nEnter the album name : ";
getline(cin, music2.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music2.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music2.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 3" << endl;
cout << "\nEnter the title : ";
getline(cin, music3.title);
cout << "\nEnter the artist's name : ";
getline(cin, music3.artist);
cout << "\nEnter the album name : ";
getline(cin, music3.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music3.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music3.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 4" << endl;
cout << "\nEnter the title : ";
getline(cin, music4.title);
cout << "\nEnter the artist's name : ";
getline(cin, music4.artist);
cout << "\nEnter the album name : ";
getline(cin, music4.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music4.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music4.releaseYear);
cout << endl << "===================================================";
cout << "\nRecord No. 5" << endl;
cout << "\nEnter the title : ";
getline(cin, music5.title);
cout << "\nEnter the artist's name : ";
getline(cin, music5.artist);
cout << "\nEnter the album name : ";
getline(cin, music5.album);
cout << "\nEnter the genre of the title : ";
getline(cin, music5.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, music5.releaseYear);
//output
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\t\tMUSIC RECORDS";
cout << endl << "================================================================================================================";
cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |";
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 1";
cout << setw(27) << left << music1.title;
cout << setw(21) << left << music1.artist;
cout << setw(20) << left << music1.album;
cout << setw(20) << left << music1.genre;
cout << setw(15) << left << music1.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 2";
cout << setw(27) << left << music2.title;
cout << setw(21) << left << music2.artist;
cout << setw(20) << left << music2.album;
cout << setw(20) << left << music2.genre;
cout << setw(15) << left << music2.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 3";
cout << setw(27) << left << music3.title;
cout << setw(21) << left << music3.artist;
cout << setw(20) << left << music3.album;
cout << setw(20) << left << music3.genre;
cout << setw(15) << left << music3.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 4";
cout << setw(27) << left << music4.title;
cout << setw(21) << left << music4.artist;
cout << setw(20) << left << music4.album;
cout << setw(20) << left << music4.genre;
cout << setw(15) << left << music4.releaseYear;
cout << endl << "----------------------------------------------------------------------------------------------------------------";
cout << endl << setw(12) << left << " 5";
cout << setw(27) << left << music5.title;
cout << setw(21) << left << music5.artist;
cout << setw(20) << left << music5.album;
cout << setw(20) << left << music5.genre;
cout << setw(15) << left << music5.releaseYear;
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\tEND OF PROGRAM, THANK YOU";
cout << endl << "================================================================================================================";
return 0;
}
As you might notice, you copy/paste code for each of your variables.
So create function to avoid to duplicate code, for example:
musicRecord inputMusicRecord(int recordNb)
{
musicRecord res;
cout << "===================================================";
cout << "\nRecord No. " << recordNb << endl;
cout << "\nEnter the title : ";
getline(cin, res.title);
cout << "\nEnter the artist's name : ";
getline(cin, res.artist);
cout << "\nEnter the album name : ";
getline(cin, res.album);
cout << "\nEnter the genre of the title : ";
getline(cin, res.genre);
cout << "\nEnter the year the title was released : ";
getline(cin, res.releaseYear);
return res;
}
void print(const musicRecord& music)
{
cout << endl << setw(12) << left << " 1";
cout << setw(27) << left << music.title;
cout << setw(21) << left << music.artist;
cout << setw(20) << left << music.album;
cout << setw(20) << left << music.genre;
cout << setw(15) << left << music.releaseYear;
}
And then, you might do:
void print_header()
{
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\t\tMUSIC RECORDS";
cout << endl << "================================================================================================================";
cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |";
}
void print_separator()
{
cout << endl << "----------------------------------------------------------------------------------------------------------------";
}
void print_footer()
{
cout << endl << "================================================================================================================";
cout << endl << "\t\t\t\t\tEND OF PROGRAM, THANK YOU";
cout << endl << "================================================================================================================";
}
int main()
{
const musicRecord music1 = inputMusicRecord(1);
const musicRecord music2 = inputMusicRecord(2);
const musicRecord music3 = inputMusicRecord(3);
const musicRecord music4 = inputMusicRecord(4);
const musicRecord music5 = inputMusicRecord(5);
print_header();
print_separator();
print(music1);
print_separator();
print(music2);
print_separator();
print(music3);
print_separator();
print(music4);
print_separator();
print(music5);
print_footer();
}
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'));
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
// STRUCTURE FOR ADD NEW DRIVER'S RECORD
struct driver
{
int long reg;
string name;
string fhname;
string peraddress;
string pesentaddress;
string dob;
string cnic;
string qualification;
string occupation;
string phone;
string mobile;
string licnum;
string email;
string city;
string gender;
string province;
string lictype;
string vitype;
}e[100];
// FUNCTION FOR ADD NEW RECORD OF DRIVER
void add_driver_record()
{
system("CLS");
char option;
fstream driverTextFile, addrecord;
addrecord.open("rcd.data", ios::out | ios::app | ios::binary);
cout << "Do you Want to Enter New Record (Y/N) ";
option = _getch();
system("cls");
if (option == 'Y' || option == 'y')
{
for (int i = 1; i == 1; i++)
{
while (option != 'N')
{
cout << "\t\t Driver Information " << endl;
cout << "\t\t++++++++++++++++++++" << endl;
cout << "\t\tEnter Token No :";
cin >> e[i].reg;
cout << "\t\tEnter License Number :";
cin.ignore();
getline(cin, e[i].licnum);
cout << "\t\tEnter Full Name :";
getline(cin, e[i].name);
cout << "\t\tEnter Father/Husband Name :";
getline(cin, e[i].fhname);
cout << "\t\tEnter Permanent Address :";
getline(cin, e[i].peraddress);
cout << "\t\tEnter Present Address :";
getline(cin, e[i].pesentaddress);
cout << "\t\tEnter Date of Birth :";
getline(cin, e[i].dob);
cout << "\t\tEnter CNI :";
getline(cin, e[i].cnic);
cout << "\t\tEnter city :";
getline(cin, e[i].city);
cout << "\t\tEnter Province :";
getline(cin, e[i].province);
cout << "\t\tEnter Gender :";
cin >> e[i].gender;
cout << "\t\tEnter Occupation :";
cin >> e[i].occupation;
cout << "\t\tEnter Qaulification :";
cin.ignore();
getline(cin, e[i].qualification);
cout << "\t\tEnter E-mail :";
cin.ignore();
getline(cin, e[i].email);
cout << "\t\tEnter Phone No :";
cin >> e[i].phone;
cout << "\t\tEnter Mobile No :";
cin >> e[i].mobile;
cout << "\t\tEnter License Type :";
cin.ignore();
getline(cin, e[i].lictype);
cout << "\t\tEnter Vehicle Type :";
cin.ignore();
getline(cin, e[i].vitype);
addrecord.write((char *)&e, sizeof(driver));
driverTextFile << "========================================================\n";
driverTextFile << "\t\t Driver Information " << endl;
driverTextFile << "\t\t+++++++++++++++++++++++" << endl;
driverTextFile << "\t\t Token No :" << e[i].reg << endl;
driverTextFile << "\t\t License Number :" << e[i].licnum << endl;
driverTextFile << "\t\t Full Name :" << e[i].name << endl;
driverTextFile << "\t\t Father/Husband Name :" << e[i].fhname << endl;
driverTextFile << "\t\t Permanent Address :" << e[i].peraddress << endl;
driverTextFile << "\t\t Present Address :" << e[i].pesentaddress << endl;
driverTextFile << "\t\t Date of Birth :" << e[i].dob << endl;
driverTextFile << "\t\t CNIC :" << e[i].cnic << endl;
driverTextFile << "\t\t City :" << e[i].city << endl;
driverTextFile << "\t\t Province :" << e[i].province << endl;
driverTextFile << "\t\t Gender :" << e[i].gender << endl;
driverTextFile << "\t\t Occupation :" << e[i].occupation << endl;
driverTextFile << "\t\t Qaulification :" << e[i].qualification << endl;
driverTextFile << "\t\t E-mail :" << e[i].email << endl;
driverTextFile << "\t\t Phone No :" << e[i].phone << endl;
driverTextFile << "\t\t Mobile No :" << e[i].mobile << endl;
driverTextFile << "\t\t Enter License Type :" << e[i].lictype << endl;
driverTextFile << "\t\t Enter Vehicle Type :" << e[i].vitype << endl;
driverTextFile << "========================================================\n";
addrecord.close();
cout << "Do you Want to Enter New Record (Y/N) ";
cin >> option;
switch (option) {
case 'Y':
case 'y':
add_driver_record();
break;
case 'N':
case 'n':
exit(1);
break;
default:
printf("Please select suitable option \n");
add_driver_record();
break;
}
} // while Loop ends
}// for loop ends
} // if condition ends
else if (option == 'N' || option == 'n')
{
printf("Please Select Suitable Option \n");
}
}
// FUNCTION FOR DISPLAY ALL RECORDS OF DRIVER'S
void display_all_driver()
{
system("CLS");
system("color f5");
fstream myfile, driverTextFile;
myfile.open("rcd.data", ios::in | ios::out | ios::binary);
int idx = 1;
while (idx == 1)
{
myfile.read((char*)&e[idx].reg, sizeof(e[idx].reg));
myfile.read((char*)&e[idx].licnum, sizeof(e[idx].licnum));
myfile.read((char*)&e[idx].name, sizeof(e[idx].name));
myfile.read((char*)&e[idx].fhname, sizeof(e[idx].fhname));
myfile.read((char*)&e[idx].peraddress, sizeof(e[idx].peraddress));
myfile.read((char*)&e[idx].pesentaddress, sizeof(e[idx].pesentaddress));
myfile.read((char*)&e[idx].dob, sizeof(e[idx].dob));
myfile.read((char*)&e[idx].cnic, sizeof(e[idx].cnic));
myfile.read((char*)&e[idx].city, sizeof(e[idx].city));
myfile.read((char*)&e[idx].province, sizeof(e[idx].province));
myfile.read((char*)&e[idx].gender, sizeof(e[idx].gender));
myfile.read((char*)&e[idx].occupation, sizeof(e[idx].occupation));
myfile.read((char*)&e[idx].qualification, sizeof(e[idx].qualification));
myfile.read((char*)&e[idx].email, sizeof(e[idx].email));
myfile.read((char*)&e[idx].phone, sizeof(e[idx].phone));
myfile.read((char*)&e[idx].mobile, sizeof(e[idx].mobile));
myfile.read((char*)&e[idx].lictype, sizeof(e[idx].lictype));
myfile.read((char*)&e[idx].vitype, sizeof(e[idx].vitype));
cout << "========================================================\n";
cout << "\t\t Driver Information " << endl;
cout << "\t\t+++++++++++++++++++++++" << endl;
cout << "\t\t Token No :" << e[idx].reg << endl;
cout << "\t\t License Number :" << e[idx].licnum << endl;
cout << "\t\t Full Name :" << e[idx].name << endl;
cout << "\t\t Father/Husband Name :" << e[idx].fhname << endl;
cout << "\t\t Permanent Address :" << e[idx].peraddress << endl;
cout << "\t\t Present Address :" << e[idx].pesentaddress << endl;
cout << "\t\t Date of Birth :" << e[idx].dob << endl;
cout << "\t\t CNIC :" << e[idx].cnic << endl;
cout << "\t\t City :" << e[idx].city << endl;
cout << "\t\t Province :" << e[idx].province << endl;
cout << "\t\t Gender :" << e[idx].gender << endl;
cout << "\t\t Occupation :" << e[idx].occupation << endl;
cout << "\t\t Qaulification :" << e[idx].qualification << endl;
cout << "\t\t E-mail :" << e[idx].email << endl;
cout << "\t\t Phone No :" << e[idx].phone << endl;
cout << "\t\t Mobile No :" << e[idx].mobile << endl;
cout << "\t\t Enter License Type :" << e[idx].lictype << endl;
cout << "\t\t Enter Vehicle Type :" << e[idx].vitype << endl;
cout << "========================================================\n";
idx++;
}
myfile.close();
}
I am so sorry if somebody else had already asked this question. But I am unable to rectify the problem that I "can not" read the binary file on console. Let me tell what my code is doing. I took structure with bunch of attribute and taking the input in that and saving that data in Binary file. And i want to read that in second method. But I am completely unable to read the data. This is my code please provide me solution. I have already tried several ways but I am still unable. Your help will save me semester project.
Your struct driver consists of std::strings, which internally represent the actual content trough a pointer to dynamically allocated memory. When you write such a struct (or an array of such structs) as plain bytes to a file, you actually write out "memory addresses" and not the actual content of the strings. So you will loose the actual content, and reading in the file will give you plain garbage.
Try the following program illustrating this:
struct TestStruct {
string testString;
};
int main(){
TestStruct ts;
cout << "sizeof ts with empty string: " << sizeof(ts) << endl;
ts.testString = "Will not get larger, right?";
cout << "sizeof ts with string set to some content: " << sizeof(ts) << endl;
}
Output:
sizeof ts with empty string: 24
sizeof ts with string set to some content: 24
Do you have read permissions for the file? That is my first thought that it is a permissions issue.
Basically I want an exact copy of the code that appears in the console window to also be outputted to a txt file..
#include <conio.h>
#include <iostream>
#include <dos.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <fstream>
using namespace std;
//Initialising gotoxy Comand
void gotoxy(int col, int row)
{
COORD coord;
coord.X = col;
coord.Y = row;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
char name1[20], name2[20], name3[30], name4[20];
int funcNum = 0;
int n1Nv, n1Mv, n1SEv, n1SWv;
int n2Nv, n2Mv, n2SEv, n2SWv;
int n3Nv, n3Mv, n3SEv, n3SWv;
int n4Nv, n4Mv, n4SEv, n4SWv;
int n1Total, n2Total, n3Total, n4Total, perTotal;
double n1Per, n1PerTotal;
double n2Per, n2PerTotal;
double n3Per, n3PerTotal;
double n4Per, n4PerTotal;
double maxVote;
//Introduction
cout << "================================================================================";
cout << " Ballot Results" << endl;
cout << " Version 2.1" << endl;
cout << " Created by Team b0nkaz" << endl;
cout << "================================================================================" << endl;
//Candidate Identification
cout << "Enter the candidates running for president" << endl << endl;
//cin.getline (workaround,30); //**
cout << "Candidate One: ";
cin.getline (name1,20);
cout << "Candidate Two: ";
cin.getline (name2,20);
cout << "Candidate Three: ";
cin.getline (name3,20);
cout << "Candidate Four: ";
cin.getline (name4,20);
cout << " " << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
cout << "Input vote numbers from each region pressing enter after each input:" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
//Input Table
//Regions
gotoxy(22,19);
cout << "North" << endl;
gotoxy(31,19);
cout << "Midlands" << endl;
gotoxy(43,19);
cout << "South East" << endl;
gotoxy(57,19);
cout << "South West" << endl;
gotoxy(69,19);
cout << "| Total" << endl;
cout << "_____________________________________________________________________|__________" << endl;
gotoxy(69,21);
cout << "|";
gotoxy(69,22);
cout << "|";
gotoxy(69,23);
cout << "|";
gotoxy(69,24);
cout << "|";
gotoxy(69,25);
cout << "|";
gotoxy(69,25);
cout << "|";
gotoxy(69,26);
cout << "|";
gotoxy(69,27);
cout << "|";
gotoxy(69,28);
cout << "|";
gotoxy(69,29);
cout << "|";
//Candidates
gotoxy(0,22);
cout << name1;
gotoxy(0,24);
cout << name2;
gotoxy(0,26);
cout << name3;
gotoxy(0,28);
cout << name4;
//Equals
cout << endl;
cout << "_____________________________________________________________________|__________" << endl;
//Vote Input
//North
gotoxy(22,22);
cin >> n1Nv;
gotoxy(22,24);
cin >> n2Nv;
gotoxy(22,26);
cin >> n3Nv;
gotoxy(22,28);
cin >> n4Nv;
//Midlands
gotoxy(31,22);
cin >> n1Mv;
gotoxy(31,24);
cin >> n2Mv;
gotoxy(31,26);
cin >> n3Mv;
gotoxy(31,28);
cin >> n4Mv;
//South East
gotoxy(43,22);
cin >> n1SEv;
gotoxy(43,24);
cin >> n2SEv;
gotoxy(43,26);
cin >> n3SEv;
gotoxy(43,28);
cin >> n4SEv;
//South West
gotoxy(57,22);
cin >> n1SWv;
gotoxy(57,24);
cin >> n2SWv;
gotoxy(57,26);
cin >> n3SWv;
gotoxy(57,28);
cin >> n4SWv;
//Total Votes
//Name1
gotoxy(72,22);
n1Total = n1Nv + n1Mv + n1SEv + n1SWv;
cout << n1Total;
//Name2
gotoxy(72,24);
n2Total = n2Nv + n2Mv + n2SEv + n2SWv;
cout << n2Total;
//Name3
gotoxy(72,26);
n3Total = n3Nv + n3Mv + n3SEv + n3SWv;
cout << n3Total;
//Name4
gotoxy(72,28);
n4Total = n4Nv + n4Mv + n4SEv + n4SWv;
cout << n4Total << endl << endl << endl;
//Percentage Calculation
perTotal = n1Total + n2Total + n3Total + n4Total;
//Candidate One
n1Per = n1Total*100;
n1PerTotal = n1Per/perTotal;
//Candidate Two
n2Per = n2Total*100;
n2PerTotal = n2Per/perTotal;
//Candidate Three
n3Per = n3Total*100;
n3PerTotal = n3Per/perTotal;
//Candidate Four
n4Per = n4Total*100;
n4PerTotal = n4Per/perTotal;
cout << "Please wait for calculation..." << endl << endl;
//Spinning Loading Line
//std::cout << '-' << std::flush;
//for(;;)
//{
//Sleep(100);
//std::cout << "\b\\" << std::flush;
//Sleep(100);
//std::cout << "\b|" << std::flush;
//Sleep(100);
//std::cout << "\b/" << std::flush;
//Sleep(100);
//std::cout << "\b-" << std::flush;
//}
//Sleeping Program
Sleep(1500); //1.5 secs
//Total Output
cout << "Candidate percentage:" << endl << endl;
//Converting To One Decimal Place
cout << fixed;
std::cout.precision(1);
//Vote Percentages
cout << name1 << " = " << n1PerTotal << "%" << endl;
cout << name2 << " = " << n2PerTotal << "%" << endl;
cout << name3 << " = " << n3PerTotal << "%" << endl;
cout << name4 << " = " << n4PerTotal << "%" << endl << endl;;
//Calculating Winnner
maxVote=n1PerTotal;
if (n2PerTotal>maxVote)
maxVote=n2PerTotal;
if (n3PerTotal>maxVote)
maxVote=n3PerTotal;
if (n4PerTotal>maxVote)
maxVote=n4PerTotal;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
//Sleeping Program
Sleep(1500); //1.5 secs
if(maxVote==n1PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name1 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n1PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n2PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name2 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n2PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n3PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name3 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n3PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n4PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name4 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n4PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
cout << "Press any key to exit..." << endl;
getch();
//system("pause");
}
I am using MS Visual Studio 2010 and any help would be great! Please note I am still very new to C++.
EDIT I would like to be able to see the output in CMD as well as have a separate txt file with the same output code. Everything that is displayed in the CMD window also copied into the txt file.
use an ofstream operator as such.
For some reason I can't seem to find the comment button or I would be involved in the discussion above... but I've edited my code below to reflect what seem to be some concerns of yours.
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("data.txt"); //Will create a new file if one is not already in existence
//You can put a static filename here or have them enter a string
//If you use a custom string your input will be "fout.open(STRING.c_str());
fout<<"Used exactly like cout from this point on";
fout.close(); //When you are done using it to close the file
}
I chose to name my ofstream operator fout because (while this is not always good to do) this way you can quickly change all of your cout's to fout's, or replicate them.
If the user enters a value and you want to spit it back out as well, you can use the ofstream operator after every cin.
You can find more information about ofstream here...
Hope this helped!
You can do this from outside of the application using tee. tee takes the output of a program and splits it into two streams (e.g. one to stdout and one to a file). Unfortunately, Windows doesn't come with a tee command but there are many implementations available.
If you want this to happen from within your program you can do the equivalent of tee using a custom ofstream. This article explains how.