Hey everyone,,,
I am new to c++ and to reading from files especially... I wrote a code where I am filling an array of strings with strings from a file and there is an error that happen during the run of the program, please have a look at the reading and an overall look at everything and tell me where I went wrong

please reply cause this is sort of a last minute error and I have to submit this code for tomorrow,,,, HELP!!

THANKS

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main ()
{
	int N, rings;//N is the test cases
	string unlockingWord, secretWord;
	char *fileName = new char;
	ifstream inFile;
	cout<<"\nEnter the number of test cases : "; cin>>N;
	cout<<"_____________________________________\n";
	for (int i=0; i<N; i++)
	{
		cout<<"\nEnter the number of rings : ";
		cin>>rings;
		cout<<"Enter the unlocking word : ";
		cin>>unlockingWord;
		cout<<"Enter the secret word with underscores and the known letter: ";
		cin>>secretWord;
		cout<<"Enter the filename that contains the rings of letter: ";
		cin>>fileName;
		inFile.open(fileName);
		//wrong input of fileName
		if(!inFile)
		{
			cout<<"Error in opening file"<<endl;
			exit(1);
		}
		string *RINGS = new string;
		string letters;
		for (int i=0; i<rings; i++)
		{
			getline(inFile,letters,'\n');
			RINGS[i]=letters;
		}
		int index=0;
		for (index=0; index<rings; index++)
		{
			if (secretWord[index]!='_')
				break;
		}
		//variable "secret" indicates the position of the known letter of the secret
		int a, b;
		for (int j=0; j<26; j++)
			if ( RINGS[index][j] == unlockingWord[j])
				a=j;
		for (int k=0 ; k<26 ; k++)
			if (RINGS[index][k] == secretWord[index])
				b=k;
		int difference = b-a;//difference between the unlocking word and the secret word
		int secretLetter;
		cout<<"THE SECRET WORD IS ";
		for (int i=0; i<rings; i++)
		{
			for (int j=0; j<26; j++)
			{
				if ( RINGS[i][j] == unlockingWord[i])
				{
					secretLetter=j+difference;
					if (secretLetter>26)
						secretLetter=secretLetter-26;
					if (secretLetter<0)
						secretLetter=secretLetter+26;
					cout<<RINGS[i][secretLetter];
				}//end of if
			}//end of for
		}// end of for
		cout<<endl<<endl;
	}//end of for
	return 0;
}

OMG, this is so urgent. Let me help you quick before the world ends.

This part :

char *fileName = new char;

That makes fileName a char variable, not an array. What you meant to
do was this :

char *fileName = new char[100];

But thats dangerous at this stage. Since you are using strings, just
do this :

string fileName;
...
 cin >> fileName;

ifstream inFile( fileName.c_str() );
...

Edited 6 Years Ago by firstPerson: n/a

Thanks a lot but the error I was talking about was in this part:

string *RINGS = new string;		
string letters;		
for (int i=0; i<rings; i++)		
{			
getline(inFile,letters,'\n');			
RINGS[i]=letters;		
}

Hi Ayesha91,

How is the order of the letters in the file?

Is each ring separated by spaces or each individual ring is on a separate line?

each ring is on a seperate line with no spaces between the letters in each rings

I think your error might be a segmentation fault.

Try this:

string RINGS[N];		
string letters;		
for (int i=0; i<rings; i++)		
{			
getline(inFile,letters,'\n');			
RINGS[i]=letters;		
}

That should work.

thanks,,but in this way the array size is not known that is why I used a pointer to let the value of rings entered by the user be the size of the array....I tried your code and it gave me an error

I attached the file,,, if you may try it please:
input:
1
5
GREEN
_P___
(file path)
<<here is were the error occurs>>

you don't have to do anything,,, but I'd appreciate it if you would try it out because am stuck and I cannot find anything wrong...

THANKS AGAIN

Edited 6 Years Ago by ayesha91: n/a

Attachments
KFZLQMDWJUSHGCEIXRAOPNVTYB
IMWZPFJBKLTNOEQDHUXGVYASRC
FAMIETZORWPSQUNGLDYBKXHCVJ
XNAKVPICQHDFWEGBRTMLZOUSYJ
ZSYFDOWIJCAKPBTXLRUNGQMVHE

Sorry I meant

string RINGS[rings];		
string letters;		
for (int i=0; i<rings; i++)		
{			
getline(inFile,letters,'\n');			
RINGS[i]=letters;		
}

Indeed we do not know the number of rings for the cryptex but once the user enters the number of rings which you do prompt him/her to do so we then know the size of the array.

This should work

Don't you realize that since this :

char *fileName = new char;

makes fileName a char variable and not an array,

then this

string *RINGS = new string;

makes RINGS a string variable and not an Array.

Again, you need this :

int max = 4;
string * arrString = new string[4];

Better yet, if you can use vectors.

std::vector< string > arrStr;

for(int i = 0; i < rings; i++){
   string tmp;
   getline( cin , tmp );
  arrStr.push_back( tmp ); //add string into our array
}
This article has been dead for over six months. Start a new discussion instead.