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.
Related
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc ,char *argv[]){
int *array;
int pl;
string filename;
if (argc == 2){
filename = argv[1];
}
fstream f(filename, ios::in| ios::out);
f >> pl;
array = new int[pl];
for(int i=0; i<pl; i++){
f>>array[i];
cout<<array[i]<<endl;
}
delete [] array;
return 0;
}
I have tried the above and it works well, but is there a better way to do it? Say, for instance, if I was trying to do it from a class method.
Your solution is perfectly fine and is along the fastest to achieve your goal. Anything else, including classes, just need more effort/typing to do the same with no advantage. I would only add a minimal error handling:
#include <iostream>
#include <string>
using namespace std;
int main(int argc ,char *argv[]){
if (argc!=2) {
cerr << "please provide a filename as argument." << endl;
return 1;
}
string const filename(argv[1]);
// etc...
// etc...
// etc...
return 0;
}
And if you will ever need anything more complex than just a single "filename" parameter, please checkout the really great cli11 https://github.com/CLIUtils/CLI11 library.
i am trying to convert the string into capital letter string by assigning single char's to string like this:-
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.reserve(a.size()+1);
for(int i=(a.size()),i1=0;1;i1++)
{
if(b[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
a[i1]='\0';
break;
}
}
cout << b <<endl;
}
every when run a.out by ./a.out ,Only endl gets prints
here is sample run:-
$ ./a.out
play clash royale
$
What is wrong in my program?? How can I assign single char to string??
There are some issues with your program. The main one is probably the diference between string reserve and string resize. What you want in your program is already had a string of a.size() length, so, use b.resize(a.size()).
A working version is bellow (there are better ways to write this, just being most consistent with OP proposal):
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.resize(a.size());
for(int i1=0; i1 < a.size();i1++)
{
if(a[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
b[i1]='\0';
break;
}
}
cout << b <<endl;
}
I am attempting to do a simple demographics input->output program. Enter persons information and it writes it to a csv file. However I can not get the name portion to work. I always get a segmentation fault. The code below is the offending bits and will not work. I know it has something to do with the strings, if I change to int it works just fine. All other data input(which has been remove for simplicity)works.
Main
#include <iostream>
#include "People.h"
using namespace std;
void demographics();
int main()
{
demographics();
return 0;
}
void demographics()
{
short elements = 2;
Names test[elements];
vector<string> name2;
for(int i =0; i<=elements; i++)
{
string name;
cout << "Please enter first name for child " << i+1 << endl;
cin >> name;
name2.push_back(name);
test[i].setName(name2);
}
return;
}
People.h
#ifndef PEOPLE_H_INCLUDED
#define PEOPLE_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Names
{
private:
vector<string> names;
public:
void setName(vector<string>&);
};
People.cpp
#include "People.h"
#include <iostream>
using namespace std;
void Names::setName(vector<string>& f_l_name)
{
names = f_l_name;
}
for(int i =0; i<=elements; i++)
Should be
for(int i =0; i < elements; i++)
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.
I'm getting a error with redo char (in bold) on main.cpp when I obviously declare it above. I also would like to know why its asking me to put a semicolon in front of using namespace std since I've never done that before.
//ReverseString.h
#include <iostream>
#include <string>
using namespace std;
class StringClass
{
public:
string string;
int GetStringLength (char*);
void Reverse(char*);
void OutputString(char*);
void UserInputString (char*);
StringClass();
private:
int Length;
}
//StringClass.cpp
#include <iostream>
#include <string>
#include "ReverseString.h"
;using namespace std;
void StringClass::UserInputString(char *string)
{
cout << "Input a string you would like to be reversed.\n";
cin >> string;
cout << "The string you entered: " << string << endl;
}
int StringClass::GetStringLength (char *string)
{
Length = strlen(string);
return Length;
}
void StringClass::Reverse(char *string)
{
int c;
char *front, *rear, temp;
front = string;
rear = string;
GetStringLength(string);
for ( c = 0 ; c < ( Length - 1 ) ; c++ )
rear++;
for ( c = 0 ; c < Length/2 ; c++ )
{
temp = *rear;
*rear = *front;
*front = temp;
front++;
rear--;
}
}
void StringClass::OutputString(char *string)
{
cout << "Your string reversed is: " << string << ".";
}
//Main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "ReverseString.h"
;using namespace std;
const int MaxSize = 100;
int main()
{
do
{
char string[MaxSize];
**char redo;**
StringClass str;
str.UserInputString(string);
str.Reverse(string);
str.OutputString(string);
//Asks user if they want redo the program
cout << "Would you like to redo the program?\n";
cout << "Please enter Y or N: \n";
**cin >> redo;**
}while(redo == 'Y' || redo == 'y');
}
It's really confusing of why its declaring it but giving an error that it isn't declared.
redo is declared as a local variable within the loop. Its scope starts at the point of declaration, and ends at the closing brace right before while keyword. The name redo is not known within the while condition.
You are missing a semi-colon after your class declaration in ReverseString.h.
The compiler is picking up the error on the line using namespace std; because that is when a problem is first detected. That doesn't mean that you should put the semi-colon there.
Some compilers will hint that you might be missing a semi-colon from a class declaration, while others will not. This mistake is quite common. If you see the missing semi-colon error in a ridiculous place, you should immediately consider that you might have accidentally left one out of your headers.