I really need your help for my C++ homework, Here is what I should do:
1: The user can create a txtfile then input
2:If the user search for that particular txtfile, the program should show the txtfile's contents
3:The user should be able to edit the contents of the txtfile while running the program
I apologize if this had been answered before but I can't find the answer, Please keep the codes as simple as possible as I am still new to programming. I really need your help. Thank you very much.
BTW, I am using Turbo C++ as compiler
Here is my codes:
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>
int main()
{
clrscr();
char name[20], course[20], age[2], num[15], address[30];
char gender[1], bday[15], fn[30], focc[20], mn[30], mocc[20];
char ans, file[30];
int x;
do{
ofstream myfile;
cout<<"File Name: ";
cin>>file;
myfile.open(file,ios_base::out);
cout<<"Student Information \n\n";
cout<<"Name: ";
myfile<<"Name: "<<gets(name);
cout<<"Gender: ";
myfile<<"\nGender: "<<gets(gender);
cout<<"Age: ";
myfile<<"\nAge: "<<gets(age);
cout<<"Address: ";
myfile<<"\nAddress: "<<gets(address);
cout<<"Birthday: ";
myfile<<"\nBirthday: "<<gets(bday);
cout<<"Course: ";
myfile<<"\nCourse: "<<gets(course);
cout<<"Father's name: ";
myfile<<"\nFather's Name: "<<gets(fn);
cout<<"Occupation: ";
myfile<<"\nOccupation: "<<gets(focc);
cout<<"Mother's Name: ";
myfile<<"\nMother's Name: "<<gets(mn);
cout<<"Occupation: ";
myfile<<"\nOccupation: "<<gets(mocc);
myfile.close();
cout<<"\n\nDo you want to edit? [y/n]";
ans = getche();
//ansy
if(ans=='y')
{
clrscr();
x=1;
}
else if (ans=='n')
{
x=0;
}
else
{
cout<<" Invalid choice";
x=0;
}
}while(x!=0);
getch();
return 0;
}
Related
I just got into programming and was doing a fun little exercise in c++ ( a login app using .dat's as a database ) and ran into a problem. Is there any way to create a "string named file" in a specific directory? I've seen that ones can create both a string named file and a file in a specific directory but cant seem to find the answer for this question. Once again sorry if this has already been gone over and thank you.
This is the part I was on about I'll put the full code down below to get some more opinions as well, just do take in mind that I'm a beginner and the code isn't finished.
int DataBase() {
ofstream DBName(( /*Potencially my directory? + */ BaseAccount + ".dat").c_str());
DBName << Pass << endl;
}
My full code will be down here, im fully open to new suggestions, yet, I do already have some parts of the code that I want to keep them as it is, again do remember that I'm a starter and thank you for your attention!
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;
int UserVar;
string Account, Pass, AccountLog;
int main(), login();
int DataBase() {
ofstream DBName(( /*Potencially my directory? + */ BaseAccount + ".dat").c_str());
DBName << Pass << endl;
}
void wait(int seconds){
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
void DataBaseCheck(string AccCheck){
ifstream DBLookForAcc(AccCheck.c_str());
cout<<"Looking for the user in the data base..."<<endl;
wait(2);
system("cls");
string AccData, AccPass, DBPass;
int DBVarTries=3;
if (DBLookForAcc){
cout<<"Account Found!"<<endl;
wait(1);
system("cls");
ifstream AccData;
AccData.open(AccCheck);
AccData>>DBPass;
do{
cout<<"Please enter your password: ";
cin>>AccPass;
if(AccPass==DBPass){
system("cls");
cout<<"You sucessfully logged in!"<<endl;
exit(0);
}
else (AccPass==DBPass);{
DBVarTries=DBVarTries-1;
system("cls");
cout<<"The password is wrong, please try again!"<<endl<<endl<<DBVarTries<<" attempts remaining. ";
wait(2);
system("cls");
continue;
}
}
while(DBVarTries!=0);
}
else {
system("cls");
cout<<"Sorry... We couldnt find your account."<<endl;
wait(1);
login();
}
}
int login(){
system("cls");
cout<<"Sign in"<<endl<<endl<<endl<<"User Name: " ;
cin>>AccountLog;
system("cls");
DataBaseCheck((AccountLog + ".dat").c_str());
}
int main(){
do{
system("cls");
cout<<"Sign up"<<endl<<endl<<endl<<"Insert your user: ";
cin>>Account;
if(Account=="1"){
do{
login();
}
while(true);
}
cout<<"Insert your accounts password: ";
cin>>Pass;
if(Pass=="1"){
do{
login();
}
while(true);
}
system("cls");
DataBase();
cout<<"The account was saved to the Data Base. ";
wait(2);
system("cls");
UserVar=UserVar+1;
}
while(UserVar=4);
cout<<"You cant sign any more users up."<<endl<<endl<< "Login protocol" ;
login();
}
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
int a;
cout<<"enter key ";
cin>>a;
if(a==1)
{
string info[0][1];
cout<<"enter your name ";
getline(cin, info[0][0]);
cout<<"enter father's ";
getline(cin, info[0][1]);
ofstream file("info.txt");
for(int i=0;i<=1;i++)
{
file<<info[0][i]<<" ";
}
file.close();
}
int b;
cout<<"enter b ";
cin>>b;
if(b==2)
{
char info[0][1];
ifstream file("info.txt");
for(int j=0;j<=1;j++)
{
file>>info[0][j];
}
cout<<endl;
for(int i=0;i<=4;i++)
{
cout<<info[0][i]<<" ";
}
}
return 0;
}
I want to store user and his father's name (other info as well which is not mentioned in code) in the form of 2d array, and ofstream that array and later ifstream the same.
in the code:
After taking input a=1, it displays "enter your name", to which it is not taking any input i.e nothing gets typed in the output window from keyboard. (no compiler error) Please help!!
I am coding in code blocks.
This is not legal C++ (because arrays of size zero are not legal).
string info[0][1];
cout<<"enter your name ";
getline(cin, info[0][0]);
cout<<"enter father's ";
getline(cin, info[0][1]);
ofstream file("info.txt");
for(int i=0;i<=1;i++)
{
file<<info[0][i]<<" ";
}
All you need is two strings, so just
string info[2];
cout<<"enter your name ";
getline(cin, info[0]);
cout<<"enter father's ";
getline(cin, info[1]);
ofstream file("info.txt");
for(int i=0;i<2;i++)
{
file<<info[i]<<" ";
}
Thne when you try to read them back, you've made the same error, and added a new one
char info[0][1];
Why did you switch to char here? Again you need two strings.
string info[2];
Finally for some reason at the end you are trying to output five strings.
for(int i=0;i<=4;i++)
{
cout<<info[0][i]<<" ";
}
Again two is the magic number
for(int i=0;i<2;i++)
{
cout<<info[i]<<" ";
}
Plus you have the problem mentioned by Yksisarvinen. You need to fix that in the way mentioned in the link.
Im talking about the string validation in void enter() , what im trying to do is create a system , where if someone accidentally puts a number in Enter name field , they need to write it again
im just not being able to solve this string validation at all.
I tried something like
after cout<<"Enter name";
for(int i=0;i<strlen(name);i++)
{
gets(name);
if(isalpha(name[i]))
cout<<"There is a number in the input , try again";
}
include<fstream.h>
include<stdio.h>
include<conio.h>
include<string.h>
include<stdlib.h>
class Directory
{
char name[20];
char num[30];
char address[50];
public:
void enter()
{
cout<<"Enter name: "<<endl;
gets(name);
cout<<"Enter number: "<<endl;
gets(num);
cout<<"Enter address: "<<endl;
gets(address);
}
it did work , but if I enter a name "mar3", now i have to enter Mark four times for it to take the record.
I think this is what you want to do. You want to reenter the name untill it meets your requrement.
bool validate(char name[])
{
for(int i=0;i<strlen(name);i++)
if(isalpha((unsigned char)name[i]))
{
cout<<"There is a number in the input , try again";
return 0;
}
return 1;
}
and while reading name do this.
gets(name);
while(!validate(name))
{
gets(name);
}
UPD :
use string and getline to read.
#include<fstream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
bool validate(string name)
{
for(int i=0;i<name.length();i++)
if(isalpha(name[i]))
{
cout<<"There is a number in the input , try again\n";
return 0;
}
return 1;
}
class Directory
{
string name,num,address;
public:
void enter()
{
cout<<"Enter name: "<<endl;
getline(cin,name);
while(!validate(name))
{
getline(cin,name);
}
cout<<"Enter number: "<<endl;
getline(cin,num);
cout<<"Enter address: "<<endl;
getline(cin,address);
}
};
here is you want to also limit the length of the name or number you can make another validator for that
I'm doing an assignment which requires file handling in C++.Assignment is long enough to be posted here, but i have made a similar simple code to elaborate my question.The following code is not read all data from file it only read the last record
please help me to make it correct.
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string.h>
//class
class medicine{
int id;
char med_name[50];
int price;
int quantity;
char date[50];
public:
void data_entery(){
//int i;char na[50];int p;int q;char da[50];
cout<<"Enter id: ";
cin>>id;
cout<<"Enter name: ";
cin>>med_name;
cout<<"Enter price: ";
cin>>price;
cout<<"Enter quantity: ";
cin>>quantity;
cout<<"Enter exp_date: ";
cin>>date;
}
void display(){
cout<<this->id<<"\t "<<this->med_name<<"\t "<<this->price<<"\t"<<this- >quantity<<"\t "<<this->date;
}
}med[10];
using namespace std;
int main (){
//writing in file
ofstream wfile("medicines.txt",ios::app|ios::binary);
wfile.seekp(0,ios::end);
int rec=wfile.tellp()/sizeof(medicine);
cout<<"there are "<<rec<<" records";
med[rec].data_entery();
wfile.write(reinterpret_cast<char *>(&med),sizeof(medicine));
wfile.close();
//reading in file
ifstream rfile("medicines.txt",ios::in|ios::binary);
rfile.seekg(0,ios::end);
rec=rfile.tellg()/sizeof(medicine);
rfile.seekg(0,ios::beg);
cout<<"there are "<<rec<<" records";
for (int k=0;k<rec;k++){
med[k].display();
}
rfile.read(reinterpret_cast<char *>(&med),sizeof(med)*rec);
rfile.close();
getch();
return 0;
}
I am assuming that your file medicines.txt is initially empty.
What you have done wrong is that you have made 10 objects of the medicine class, but you are writing only one object to the file.
What you need to do here is:
for (int i=0; i < 10; i++) {
med[i].data_entery();
wfile.write(reinterpret_cast<char *>(&med),sizeof(medicine));
}
wfile.close();
Now you'll need to add 10 medicines, after details of each medicine has been added, that medicine will be written to the file.
I hope this helps.
Here is a better version of your code
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string.h>
using namespace std;
//class
class medicine
{
int id;
char med_name[50];
int price;
int quantity;
char date[50];
public:
void data_entery()
{
//int i;char na[50];int p;int q;char da[50];
cout<<"\nEnter id: ";
cin>>id;
cout<<"Enter name: ";
cin>>med_name;
cout<<"Enter price: ";
cin>>price;
cout<<"Enter quantity: ";
cin>>quantity;
cout<<"Enter exp_date: ";
cin>>date;
}
void display()
{
cout<<"\nID :"<<this->id<<"\nName :"<<this->med_name<<"\nPrice :"<<this->price<<"\nQuantity :"<<this->quantity<<"\nExp Date :"<<this->date<<"\n";
}
}med;
int main ()
{
//writing in file
ofstream wfile("medicines.txt",ios::app|ios::binary);
wfile.seekp(0,ios::end);
int rec=wfile.tellp()/sizeof(medicine);
cout<<"\nthere are "<<rec<<" records";
med.data_entery();
wfile.write(reinterpret_cast<char *>(&med),sizeof(medicine));
wfile.close();
//reading in file
ifstream rfile("medicines.txt",ios::in|ios::binary);
rfile.seekg(0,ios::end);
rec=rfile.tellg()/sizeof(medicine);
rfile.seekg(0,ios::beg);
cout<<"\nthere are "<<rec<<" records";
while(rfile.read(reinterpret_cast<char *>(&med),sizeof(med)))
{
med.display();
}
rfile.close();
getch();
return 0;
}
Now, when you are using files, you do not need to create an array of objects. Once you store the object into the file, you can just reuse that object. The data can be accessed from the File. That is what I have done in this code. I have edited this code in such a manner as to not change your method, instead just corrected the problems and also made your code a little better. In each run of this code, you enter the details of one record, and then the details of all the records are shown.
The good thing about this code is, you can enter as many records as you want. You are not restricted to 10 records like your original code.
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 8 years ago.
Greetings fellas;
So after i compile the following code ,the MusicIns.shortDescription object don t want to receive any input and just switching to the next object ,what id wrong in my code ?
#include <iostream>
using namespace std;
class Instruments{
public :
int weight;
string size;
string name;
string shortDescription;
string designation;
};
int main(){
Instruments MusicIns;
cout<<"Please enter instrument name: "<<endl;
getline(cin, MusicIns.name);
cout<<"Please enter the size"<<endl;
getline(cin, MusicIns.size);
cout<<"Please enter the weight: "<<endl;
cin>>MusicIns.weight;
cout<<"Please enter a small description: "<<endl;
getline(cin, MusicIns.shortDescription);
cout<<endl;
cout<<"Please describe the instrument designation: "<<endl;
getline(cin, MusicIns.designation);
cout<<endl;
cout<<"============="<<endl;
cout<<MusicIns.name <<endl;
cout<<MusicIns.weight <<endl;
cout<<MusicIns.designation <<endl;
cout<<MusicIns.shortDescription <<endl;
cout<<MusicIns.size <<endl;
return 0;
};
Don t blame me too much for my level or syntax i am still noob.
P.s. Thank you in advance for any help !
After cin>>MusicIns.weight;, there is a newline remaining in the stream. So the new line will be assigned to the MusicIns.shortDescription. Add this line to ignore the newline:
cin.ignore(100, '\n'); before std::getline(std::cin, MusicIns.shortDescription);
One way you can do have "MusicIns.shortDescription" to receive an input is to do this method.
cin.get();
This method will catch any characters after you have terminated from the input.
cout<<"Please enter instrument name: "<<endl;
getline(cin, MusicIns.name);
cout<<"Please enter the size"<<endl;
getline(cin, MusicIns.size);
cout<<"Please enter the weight: "<<endl;
cin>>MusicIns.weight;
cout<<"Please enter a small description: "<<endl;
std::getline(std::cin, MusicIns.shortDescription);
cin.get();// new line
cout<<endl;
cout<<"Please describe the instrument designation: "<<endl;
getline(cin, MusicIns.designation);
cin.get();// new line
cout<<endl;