I am learning c++ by myself. I just try to do a library program. But something is wrong. after the user select number from menu, program turns off. Here is my code:
Here is my menu.cpp:
using namespace std;
class Menu{
public:
int menuchoice();
};
int Menu::menuchoice()
{
int choice;
cout<<"1 - Kitaplarim"<<endl;
cout<<"2 - Kitap Ekle"<<endl;
cout<<"3 - About coder"<<endl;
cout<<"4 - Exit"<<endl;
cout<<"Bir secenek secin";
cin>>choice;
return choice;
}
Here is my main.cpp
#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<string>
#include "menu.cpp"
using namespace std;
class Kitap{
public:
string Ad;
void takeinfobook();
};
void Kitap::takeinfobook(){
cout<<"Kitabin adi...:";
std::getline(std::cin, Ad);
ofstream savefile("savebook.txt");
savefile<<Ad;
cout<<Ad;
}
main(){
Menu menu;
int choice = menu.menuchoice();
if(choice==2)
{
Kitap book;
book.takeinfobook();// After this line program must take me a book name and write to file. But it doesnt. Program turns off..
}
else
{
cout<<"hodor";
}
}
The issue you are seeing is answered here: Need help with getline()
void Kitap::takeinfobook(){
cout << "Kitabin adi...:";
ws(cin); // <--- Add this to make getline block.
std::getline(std::cin, Ad);
ofstream savefile("savebook.txt");
savefile << Ad;
cout << Ad;
}
Also, as an aside, never include a cpp into another cpp file!! You need to use a header; you should create a file called 'menu.h', containing:
#pragma once // or other include guard depending on your setup
class Menu{
public:
int menuchoice();
};
Related
The code below,works fine but it does not take any value for age and terminates.`
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
char name[20],server[40];
public:
void get(){
cout<<"Enter your name:";
cin>>name[20];
cout<<"Enter your age:";
cin>>age;
}
};
int main(){
user u;
u.get();
return 0;
}
/*Output
Enter your name:Jack
Enter your age:
C:\Users\user\documents\c++
*/
In the output section ,age is not accepted and the program terminates.
Use string name instead of char name[20] to take multi-character value. char name[20] will terminate after taking a single character.
Also, its valued will not be displayed on giving output.
Modified code for reference.
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
string name,server;
public:
void get(){
cout<<"Enter your name:";
cin>>name;
cout<<"Enter your age:";
cin>>age;
}
//test output
void put(){
cout<<name<<endl;
cout<<age<<endl;
}
};
int main(){
user u;
u.get();
//test
u.put();
return 0;
}
Just modify the code to this :
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
char name[20],server[40];
public:
void get(){
cout<<"Enter your name:";
cin>>name; // changes done here
cout<<"Enter your age:";
cin>>age;
}
};
int main(){
user u;
u.get();
return 0;
}
Job Done :)
Your problem is here:
cin>>name[20];
Why:
'name[20]' is 21th char of the array you defined before. It counts from 0! As this, it is simply a single char. If you now enter more than a single char, the rest is read by the cin>>age.
Example:
cout<<"Enter your name:";
cin>>name[20];
cout<<"Enter your age:";
cin>>age;
std::cout << "Name " << name << std::endl;
std::cout << "Age " << age << std::endl;
And entering:
Enter your name:1234
Enter your age:Name
Age 234
As you see, the '1' is now in the name and the rest is stored in age.
But attention: You defined your array as `name[20], which means you have 0..19 elements. Accessing name[20] is wrong!
But what you simply want to do was:
cin >> name;
The easiest way to handle strings (a long sequence of characters) or even the strings that have spaces just use the following library in C++.
#include <bits/stdc++.h>
Then just declare a string variable.
String name;
Now you can save a very long string without any error. e.g.
name = jshkad skshdur kslsjue djsdf2341;
and you'll get no error, enjoy ;)
I'm trying to build a beginner employee database program.
Here is the problem; when counter "i" in main() becomes "1", the 1st line of the loop is skipped; in the other words, it doesn't wait for user to enter the value for "name" string. When I use "cin" rather than "gets" there is no problem...Why this is so? I use ubuntu 16.04.
#include <iostream>
#include <cstdio>
using namespace std;
void enter();
void report();
char name[2][30],salary[2][30];
int main()
{
int i;
for(i=0;i<2;i++){
cout<< "Name:"<<'\n';
gets(name[i]);
cout<< "Salary:"<<'\n'; cin>>(salary[i]);
}
report();
return 0;
}
void report()
{
int i;
cout<<"Name"<<'\t'<<"Salary"<<'\n';
for(i=0;i<2;i++){
cout<< name[i]<<'\t'<<salary[i]<<'\n';
}
}
Instead of using gets(), I would recommend using either std::cin >> name[i]; or cin.getline(name[i], 30);. The latter will fetch spaces.
You will then need a cin.ignore(); after the cin >> salary[i]; because of the extra return character that fills the buffer.
I encountered a problem where gets() is not working sometimes without any error compiling. In other words, gets() will not return any value but no warning or error explanation.
Here is the code where it's not returning value
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"Admission number "<<admno<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
And in contrast here is the code where it's returning value to "sname"
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
If anything is unclear don't hesitate to ask me! I'm glad to accept ant answer/solution/advise!
Use cin.ignore() before taking string input.I try this and it works fine.
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cin.ignore();
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"\nAdmission number "<<admno<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
class Student{
private:
char name[40];
char grade;
float marks;
public:
void getdata();
void display();
};
void Student::getdata(){
char ch;
cin.get(ch);
cout<<"Enter name : ";
cin.getline(name,40);
cout<<"Enter grade : ";
cin>>grade;
cout<<"Enter marks : ";
cin>>marks;
cout<<"\n";
}
void Student::display(){
cout<<"Name : "<<name<<"\t";
cout<<"Grade : "<<grade<<"\t";
cout<<"Marks : "<<marks<<"\t"<<"\n";
}
int main(){
system("cls");
Student arts[3];
fstream f;
f.open("stu.txt",ios::in|ios::out|ios::binary);
if(!f){
cerr<<"Cannot open file !";
return 1;
}
for(int i=0;i<3;i++){
arts[i].getdata();
f.write((char*)&arts[i],sizeof(arts[i]));
}
f.seekg(0); //The question is about this statement;
cout<<"The contents of stu.txt are shown below : \n";
for(int j=0;j<3;j++){
f.read((char*)&arts[j],sizeof(arts[j]));
arts[j].display();
}
f.close();
return 0;
}
The above program reads and writes objects of Student from/to the file "stu.txt".
It runs fine. But it runs fine even if I switch off the fin.seekg(0) statement. I don't understand this part ? Are we not supposed to set the file pointer to the beginning of the file - before starting to read objects from the file(in the context of this particular program)?.
If you are at the end of the file, the read method call will fail, thus the Student structures are left untouched (so you are just printing again the same structures which you left untouched after writing them to file).
#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Student{
private:
char name[40];
char grade;
float marks;
public:
void getdata();
void display();
char* getname(){return name;}
void search(fstream,char*);
};
void Student::getdata(){
char ch;
cin.get(ch);
cout<<"Enter name : ";
cin.getline(name,40);
cout<<"Enter grade : ";
cin>>grade;
cout<<"Enter marks : ";
cin>>marks;
cout<<"\n";
}
void Student::display(){
cout<<"Name : "<<name<<"\t";
cout<<"Grade : "<<grade<<"\t";
cout<<"Marks : "<<marks<<"\t"<<"\n";
}
void search(fstream fin,char* nm)/*initializing argument 1 of 'void search(std::fstream, char*)'*/{
Student s;
fin.open("stu.txt",ios::in|ios::binary);
while(!fin){
fin.read((char*)&s,sizeof(s));
if(s.getname()==nm){
cout<<"Record found !";
s.display();
break;
}
}
fin.close();
}
int main(){
system("cls");
char nam[40];
Student arts[3];
fstream f;
f.open("stu.txt",ios::in|ios::out|ios::binary);
if(!f){
cerr<<"Cannot open file !";
return 1;
}
for(int i=0;i<3;i++){
arts[i].getdata();
f.write((char*)&arts[i],sizeof(arts[i]));
}
f.close();
cout<<"Enter name to be searched for : ";
cin.getline(nam,40);
char* p = new char[40];
p=nam;
search(f,p);/*synthesized method 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' first required here*/
getch();
f.close();
return 0;
}
The above program first creates a file "stu.txt" and writes user-given input to the file.
It is then supposed to search for a record based on the name the user enters ( using the search() function ). I'm getting errors when calling search() and also defining search(). I've put in the errors the compiler throws as comment lines. Could anyone explain what's going wrong in there ?
fstream is not copyable, so you have to pass fstream as a reference, or in c++11, move it.
Given you access f after calling search, it is best to pass it by reference.
Change your function to accept the fstream as a reference:
void search(fstream& fin,char* nm)