C++ passing strings causing segmentation fault - c++

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++)

Related

How to assign char to string object in c++?

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;
}

Reading arrays from file c++

I'm trying to make a program that so far only needs to read a file and saves its content in an array. the cout was a test to see if the words would be saved to the array but it didn't work. When executed all it does is print to screen empty spaces and finally the name of the file.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <streambuf>
#include <ctime>
#include <time.h>
#define MAX 10000
void readFile(fstream& wordFile, string words[], int &wordarrayLength)
{
string word;
int i=0;
while(i < MAX)
{
getline(wordFile,word);
words[i] = word;
i++;
cout << words[i] << endl;
}
wordarrayLength = i;
wordFile.close();
}
int main()
{
string words[MAX];
int arraylength;
fstream file ("words.txt", ios::in);
readFile(file,words,arraylength);
}
I was missing the file the compiler was looking for. This code works fine.
Try something like this:
//since you are updating the array pass it in by reference as well.
void readFile(fstream& wordFile, string &words[], int &wordarrayLength)
{
int i=0;
while(i < MAX)
{
//each item in the array has to be it's own string
//the previous code simply reused the same string each time
string word;
getline(wordFile, word);
words[i] = word;
cout << words[i] << endl;
i++;
}
wordarrayLength = i;
wordFile.close();
}
Unfortunately I don't have your file or compiler so I can't debug it.

Ask user to enter his/her name, and then reverse the given name and show it on screen. using character array

My code is this But i am getting garbage values. how do i make it general so that anyone can use it?
#include <iostream>
#include <string>
using namespace std;
void main()
{
char ch[31];
cin.get(ch,31);
for (int i = 30; i >= 0; i--)
{
cout << ch[i];
}
system("pause");
}
You are taking garbage char from you empty cells.
You must first know the number of char inserted by the user
use reverse function from algorithm header file and use string instead of char.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s;
getline(cin,s);
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;
}

C++ segmentation fault on programming exercise

I am getting a segmentation fault error when trying to solve this programming marathon C++ exercise but I can't find the error anywhere:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(void)
{
int m,n,i,di,x,y;
char* let;
cin >> n >> m;
x=0;
y=0;
for (i = 0; i < n; i++)
{
cin >> let >>di;
if ((strcmp(let,"S"))||(strcmp(let,"O"))){
di=(-di);
}
if ((strcmp(let,"N"))||(strcmp(let,"S")))
{
x=+di;
}
if ((strcmp(let,"L"))||(strcmp(let,"O")))
{
y=+di;
}
if ((y*y)+(x*x)>(m*m))
{
cout << "1";
return 0;
}
}
cout << "0";
return 0;
}
This code:
char* let;
cin >> let
stores user input to the memory pointed to by let.
This is misuse of an uninitialized pointer. cin trusts that you have pointed it to valid memory, but you haven't assigned anything to it. Where it points to is unknown.
The easiest solution would be to change let to a proper C++ std::string.

Taking data from a file and putting it into a struct

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.