Here is the whole code.
I know that the problem is somewhere in the hozzaad function, but I don't know to debug it.
I want to create a dynamic 2d array, with exacly vagonszam columns, but the number of rows differ.
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;
class Vonat{
int vagonszam, maxtomb;
int **vagonok;
int *seged;
enum szallitmany {szen, fa, ko} ;
public:
void beolvas(int x);
Vonat(): vagonszam(0), maxtomb(0) {}
void kiir();
void hozzaad(int **vagonok, int i, string szallitmany);
};
void Vonat::hozzaad(int **vagonok, int i, string szallitmany)
{
++vagonok[i][0];
seged = (int *) realloc(vagonok[i], ((vagonok[i][0]+1)*sizeof(int)));
seged[vagonok[i][0]]=1;
}
void Vonat::beolvas(int x)
{
char szovegesallomany[]="beVagonTartalma";
szovegesallomany[15]=x+'0';
szovegesallomany[16]='\0';
strcat(szovegesallomany, ".txt");
ifstream f(szovegesallomany);
f>>vagonszam>>maxtomb;
vagonok = new int*[vagonszam];
string szallitmany;
for(int i=0;i<vagonszam;i++)
{
vagonok[i] = new int[1];
vagonok[i][0]=0;
do
{
f>>szallitmany;
cout<<szallitmany<<" ";
hozzaad(vagonok, i, szallitmany);
}
while(szallitmany!="*");
}
f.close();
}
void Vonat::kiir()
{
cout<<vagonszam<<" "<<maxtomb<<endl;
}
int main()
{
Vonat v_52164;
v_52164.beolvas(1);
v_52164.kiir();
Vonat v_54587;
v_54587.beolvas(2);
v_54587.kiir();
return 0;
}
In this code something's wrong, because after running it gets the error message specified before.
Thanks to everyone, who try to help me, all the best.
Related
#include <iostream>
#include <string.h>
#include <string>
#include<fstream>
struct time
{
int hours, minutes;
};
struct moviedata
{
string moviename, genre, actorname1, actorname2;
int yearreleased, priceperday;
time duration;
};
void readmoviedata(moviedata *ptr) /*not ale to run this code*/
{
int i = 0;
string x;
ifstream inside("movies.txt");
while (!inside.eof())
{
getline(inside, ptr[i].moviename);
inside >> ptr[i].yearreleased;
getline(inside, ptr[i].genre);
inside >> ptr[i].duration.hours;
inside >> ptr[i].duration.minutes;
getline(inside, ptr[i].actorname1);
getline(inside, ptr[i].actorname2);
inside >> ptr[i].yearreleased;
i++;
}
}
int main()
{
moviedata *md = new moviedata;
readmoviedata(md);
delete md;
return 0;
}
can anyone tell me what i did wrong with function readmoviedata
$ when runned line by line as soon as the function readmoviedata is called
$ black grid appears with cursor blinking and nothin else
Problem is that you are creating the one pointer variable of struct type.
moviedata *md = new moviedata;
and you are treating this variable like array.
ptr[i].yearreleased;
I still do not have much knowledge in C ++, I would like to ask for help for a task. I must create a stack, which is filled with data entered by the keyboard and the entire stack to write in external stack. I have made functions push, pop and simple program that displays the stack but before that data must be written in an external file. Can anybody help me with the external file?
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct elem
{ int key; elem *next;} *start=NULL, *p;
void push(int n)
{
p=start;
start=new elem;
start->key=n;
start->next=p;}
int pop(int &n){
if (start)
{
n=start->key;
p=start;
start=start->next;
delete p;
return 1;
}
else
return 0;
}
int main(){
int num;
cout<<"Input integers:"<<setw(10);
while (cin>>num)
{
push(num);
}
cout<<endl<<"Stack:"<<endl;
while(pop(num))
{
cout<<num<<endl;
}
return 0;
}
//you can use this pseudocode
ofstream outFile;
while(start!=-1){
outFile << pop() << endl;
I have the following C++ code for practising sequence list and it passed the complier. However, when I try to run it, it returns Segmentation fault. Please help!! Thanks a lot.
main.cpp
#include <iostream>
#include <string>
#include "SeqList.h"
using namespace std;
int main() {
SeqList seq;
string vv[] = {"a", "b", "c", "d"};
for (int i = 0; i< 4; i++) {
seq.addElement(vv[i], i);
}
string* v = seq.getSeq();
for (int i=0; i<seq.getSeqSize(); i++) {
cout << v[i] <<endl;
}
return 0;
}
SeqList.h
#include<iostream>
#include<string>
using namespace std;
class SeqList {
private:
string seq[];
int size;
public:
void addElement(string, int);
void delElement(string, int);
string* getSeq();
int getSeqSize();
};
SeqList.cpp
#include <iostream>
#include <string>
#include "SeqList.h"
using namespace std;
string seq[100];
int size = 0;
string* SeqList::getSeq(){
return seq;
};
int SeqList::getSeqSize(){
return size;
};
void SeqList::addElement(string str, int pos) {
int i;
for (i = size; i > pos; i--) {
seq[i] = seq[i-1];
}
seq[i-1] = str;
size++;
};
Your segfault is happening because you're trying to access seq[i-1] in addElement when i = 0. This tries to access the memory outside of seq which causes a segfault. Try using seq[i] and seq[i+1] instead of seq[i-1] and seq[i], though you'll have to make sure you never call that code with more than 99 values or you'll run into a similar problem where the program tries to access memory past the end of seq.
Also, in SeqList.cpp
string seq[100];
int size = 0;
These lines are creating new variables, when it looks like you're trying to change the values you made in SeqList.h. To change those private values in your class you should either use a constructor or other function to initialize the values.
I have a program that is meant to take data from 2 separate text files, place them in arrays, compare them to each other, and output certain results. However, when I read the data from the files and try to display the data, I get a lot of weird symbols before it finally displays all the data in the text files. Here is the code.
// Ch07-Exam Grader.cpp : Defines the entry point for the console application.
//
//Libraries
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Prototypes
void initialization(void);
void proccess(void);
void eoj(void);
void writeIt(void);
void readIt(void);
void calculate(void);
//Global Variables
ifstream student;
ifstream correct;
int main()
{
initialization();
return 0;
}
//This function opens the files and calls the function to send the data into the array
void initialization (void){
correct.open("CorrectAnswers.txt");
readIt();
student.open("StudentAnswers.txt");
readIt();
}
void proccess (char c[], char s[], int length){
int correctCount = 0;
int incorrectCount = 0;
for (int i = 0; i< length; i++){
if (s[i] == c[i]){
correctCount = correctCount + 1;
} else {
incorrectCount = incorrectCount + 1;
}
}
}
void eoj (void){
}
void writeIt (void){
}
//This function will take the data and place it into seperate arrays
void readIt (void){
char studentArray[20]; //Array to hold the student answers
char correctArray[20]; //Array to hold the correct answers
//Loops to place data to seperate arrays
for (int i = 0; !correct.eof(); i++){
correct >> correctArray[i];
}
for (int j = 0; !student.eof(); j++){
student >> studentArray[j];
}
for (int i = 0; i < 20; i++){
cout << studentArray[i] <<endl;
}
proccess(correctArray, studentArray, 20);
}
void calculate (void){
}
And this is the result:
Only the letters are a part of the text file.
Why do you call readIt() twice in initialization() function? It looks like readIt() expects that both files are open, but you open first file, call readIt(), open second file, call readIt() again. May be the reason of the problem in this defect?
Hello i'm trying to program the addition of 2 matrices into a new one (and it does when i run the program step by step) but for some reason VS 2010 gives me an access error after it does the addition.
Here is the code.
#include <iostream>
#include <cstdio>
#include <conio>
using namespace std;
class operatii
{
typedef double mat[5][5];
mat ms,m1,m2;
int x1,x2,y1,y2;
public:
void preg();
int cit_val();
void cit_mat(int&,int&,double[5][5]);
void suma();
void afisare(int&,int&,double[5][5]);
};
void operatii::preg()
{
cit_mat(x1,y1,m1);
cit_mat(x2,y2,m2);
suma();
afisare(x1,y1,ms);
}
int operatii::cit_val()
{
int n;
cin>>n;
return n;
}
void operatii::cit_mat(int& x,int& y,double m[5][5])
{
char r;
cout<<"Matrice patratica? Y/N ";
cin>>r;
if ((r=='y')||(r=='Y'))
{
cout<<"Numar linii si coloane: ";
x=cit_val();
y=x;
}
else
{
cout<<"Numar linii: ";
x=cit_val();
cout<<"Numar coloane: ";
y=cit_val();
}
for (int i=1;i<=x;i++)
for (int j=1;j<=y;j++)
cin>>m[i][j];
}
void operatii::suma()
{
if ((x1==x2)&&(y1==y2))
for (int i=1;i<=x1;i++)
for (int j=1;i<=y1;j++)
ms[i][j]=m1[i][j]+m2[i][j];
else cout<<"Eroare";
}
void operatii::afisare(int& x,int& y,double m[5][5])
{
cout<<endl;
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
cout<<m[i][j];
cout<<endl;
}
}
void main()
{
operatii matrice;
matrice.preg();
system("PAUSE");
}
Any kind of help would be apreciated.
Arrays are 0-based in c++.
Change your various variants of for (somevar=1; somevar<=something) to for (somevar=0; somevar<something)
You're writing past the end of your arrays, which overwrites stack return address, leading to a return to nonexecutable code, again leading to an access violation.
Also,
for (int j=1;i<=y1;j++)
I think you want to use j not i here. Such errors are much easier to see if you use longer and more distinct variable names than "i" and "j", such as e.g. "Line" and "Column"