Hi! I have made an access code program where you store access codes into a file via the program. I successfully made the program, but when the data is stored, the first letter is always deleted. how can I fix this?
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <fstream>
usingnamespace std;
char szstring[100];
char szstring2[200];
char szstring3[500];
void notes(){
ofstream data;
cout<<"Do you want to leave any notes with this? Type in 1 for yes and any other number for no: ";
int choice;
cin>> choice;
switch (choice)
{
case 1:
cout<<"\n\n\n\n\n";
cout<<"NOTES: ";
data.open("ac.txt", fstream::in|fstream::app);
cin>> szstring3[500];
cin.getline(szstring3, 500);
data<<"NOTES: ";
data<<szstring3;
data<<"\n\n\n";
data<<"==============================================================================================================\n\n";
cout<<"\n\n\n"
<<"Thank you for using this application.\n\n\n"
<<"RECOVERY CODE: 102983746\n\n";
break;
default:
cout<<"\n\n\n"
<<"Thank you for using this application.\n\n\n";
data.open("ac.txt", fstream::in|fstream::app);
data<<"===============================================================================================================";
break;
}
}
int main(){
ofstream data;
cout<<"This program stores access codes in the file :'ac.txt'"
<<"\nPlease note that at the start of each feild, enter '#' and type."
<<endl
<<endl
<<endl;
cout<<"Enter your code details;\n\n"
<<"This is the code for: ";
data.open("ac.txt", fstream::in|fstream::app);
cin>> szstring[100];
cin.getline(szstring, 100);
data<<"NAME: "<<szstring;
data<<"\n\n";
data.close();
cout<<"\n\n"
<<"Now enter the code: ";
data.open("ac.txt", fstream::in|fstream::app);
cin>>szstring2[100];
cin.getline(szstring2, 100);
data<<"CODE: "<< szstring2;
data<<"\n";
data.close();
cout<<"\n\n";
notes();
system("PAUSE");
return 0;
}
it is wrong on so many levels... What happens:
First line: You take first character from user input and store it at szstring3 in 500th cell - outside memory area allocated to szstring3! Can lead to unexpected behavior sometime.
Second line: you take the rest of using input, store it into szstring3 and append a terminating zero.
You are effectively discards first character from user input.