I don't understand why my code won't compile. I saw another post that had the same issue, but their problem was not using #include and not using namespace std, both of which I have. Anyone know why?? p.s. my code is NOT finished, but I want to make it work before I get any further.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Function prototypes to warn main function
void readData(int num);
void oddTally(int num);
void oddSum(int num);
void oddAverage(double num);
void evenTally(int num);
void evenSum(int num);
void evenAverage(double num);
void displayNames(string name1, string name2);
int main()
{
int inData, oTally, oSum, eTally, eSum;
double oAverage, eAverage;
string name1, name2;
ifstream inFile;
readData(inData);
oddTally(oTally);
oddSum(oSum);
oddAverage(oAverage);
evenTally(eTally);
evenSum(eSum);
evenAverage(eAverage);
system("pause");
return 0;
}
void readData(int num)
{
ifstream inFile;
inFile.open("in.txt");
int inData;
//Read the data.
if (inFile)
{
while (inFile >> inData)
{
cout << inData << '\n';
}
}
else
{
//Displays error message.
cout << "Error opening the file. \n";
}
}
You need:
#include <cstdlib>
to use system.
Your problem is that none of your variables is initalized but you try to pass them by value. This would result in udnefined behaviour, and this is why Visual studio complains.
Just initialize each of your variabl (for example =0) before using it.
Related
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 want the user to enter the name of a file, and if the file exists, print out all the contents of the file.
At the moment the uncommented code, takes a name of a file that the user inputs, for example. example.txt and prints out most (not the last word?) of the file. I've tried to implement this instead by using string (commented code is attempt) but clearly its incorrect.
I also wondering if i can automatically add .txt to the end of the user input, so that the console could ask - "which subject should we find more information on" user inputs "math" and it will open "math.txt"
Here is what I´ve tried:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char filename[50];
//string getcontent;
ifstream name;
cin.getline(filename, 50);
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
char word[50];
name >> word;
while (name.good()) {
cout << word << " ";
name >> word;
}
//if (!name.is_open()) {
//while (! filename).eof())
//{
//getline(name, getcontent)
//cout << getcontent << endl;
//}
//exit(EXIT_FAILURE); //comes from cstdlib
//}
//}
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string filename;
string getcontent;
ifstream name;
cin >> filename;
filename.append(".txt"); // add extension.
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
while (true)
{
getline(name, getcontent);
if (name.eof()) break;
cout << getcontent << endl;
}
return 0;
}
I found this and it helped me with a somewhat different problem and I also thought that I might be able to help. This is coded in windows. (I'm a beginner so forgive me if I made some obvious mistakes)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin;
int main()
{
//char filename[50],word[50];
string filename,word;
//cin.getline(filename,50);
getline(cin,filename);
//strcat(filename,".txt");
filename.append(".txt");
fin.open(filename);
if(fin.is_open())
while(fin>>word)
cout<<word<<endl;
else
cout<<"No such file"<<endl;
return 0;
}
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();
};
#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)
SO I'm trying to get this particular program to open a file, put those elements into a struct and then output one of the variables (to see if its working). Unfortunately I can't even start the program because my void main is telling me that it has changed to an int and then says main must return int. I am a C++ novice and as such do not realize that it could be a simple mistake with the main. However, the struct I am not to sure if it is working correctly for the strings. Sample text in the file:
surname bloodtype organ age year(admited)
Casby A heart 35 2012
Jorde B kidney 20 2009
etc....
I would be very grateful for any help towards this program as this will allow me to do the rest of the actual program (comparing two of the variables to be ==/displaying the lowest year...
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <iomanip.h>
using namespace std;
ifstream patientin;
struct Patient {
string surname;
char Btype;
string organ;
int age, year;
};
void open(){
patientin.open("patient.txt");
if (patientin == NULL){
cout <<"\nCan't open the file. Restart." << endl;
exit(1);
}
}
void close(){
patientin.close();
}
void getFileInfo(){
const int Max = 4;
int i = 0;
Patient records[Max];
while (i <= Max){
patientin >> records[i].surname;
patientin >> records[i].Btype;
patientin >> records[i].organ;
patientin >> records[i].age;
patientin >> records[i].year;
}
cout << records[0].surname << endl;
}
void main (){
open();
getFileInfo();
close();
}
Your first of many problems lies here:
void main ()
Main must return int. Some compilers may let you get away with void, but that is non-standard.
int main() { ... }
Or
int main(int argc, char** argv) { ... }
are the 2 standard signatures for main.