I'd to write a c++ project for my school that read and converts numbers in D:\Data\Input.txt to words and write them at bottom of Input.txt
I'd wrote the following code but it doesn't run good, its output for numbers below 15 isn't correct for example it converts 5 to two or 6 to twelve, etc.
I don't know how to solve its problems.
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<fstream>
using namespace std;
int q=0;
char ch[100][100];
char out[5000];
void first_digit(int yeki)
{
if (yeki<20)
switch (yeki)
{
case 1:
strcpy(ch[q],"one");
break;
case 2:
strcpy(ch[q],"two");
break;
case 3:
strcpy(ch[q],"three");
break;
case 4:
strcpy(ch[q],"four");
break;
case 5:
strcpy(ch[q],"five");
break;
case 6:
strcpy(ch[q],"six");
break;
case 7:
strcpy(ch[q],"seven");
break;
case 8:
strcpy(ch[q],"eight");
break;
case 9:
strcpy(ch[q],"nine");
break;
case 10:
strcpy(ch[q],"ten");
break;
case 11:
strcpy(ch[q],"eleven");
break;
case 12:
strcpy(ch[q],"twelve");
break;
case 13:
strcpy(ch[q],"thirteen");
break;
case 14:
strcpy(ch[q],"fourteen");
break;
case 15:
strcpy(ch[q],"fifteen");
break;
case 16:
strcpy(ch[q],"sixteen");
break;
case 17:
strcpy(ch[q],"seventeen");
break;
case 18:
strcpy(ch[q],"eighteen");
break;
case 19:
strcpy(ch[q],"nineteen");
break;
}
q++;
}
void second_digit(int dahi)
{
int a,b;
a=(dahi/10)%10;
b=dahi%10;
first_digit(b);
if (b!=0)
{
strcpy(ch[q]," - ");
q++;
}
switch (a)
{
case 2:
strcpy(ch[q],"twenty");
break;
case 3:
strcpy(ch[q],"thirty");
break;
case 4:
strcpy(ch[q],"forty");
break;
case 5:
strcpy(ch[q],"fifty");
break;
case 6:
strcpy(ch[q],"sixty");
break;
case 7:
strcpy(ch[q],"seventy");
break;
case 8:
strcpy(ch[q],"eighty");
break;
case 9:
strcpy(ch[q],"ninety");
break;
}
q++;
}
void third_digit(int sadi)
{
int a,b;
a=sadi/100;
b=sadi-(a*100);
if(b<20)
first_digit(b);
if(b>19)
second_digit(b);
if(b!=0)
{
strcpy(ch[q]," and ");
q++;
}
switch(a){
case 1:
strcpy(ch[q],"one hundred");
break;
case 2:
strcpy(ch[q],"two hundred");
break;
case 3:
strcpy(ch[q],"three hundred");
break;
case 4:
strcpy(ch[q],"four hundred");
break;
case 5:
strcpy(ch[q],"five hundred");
break;
case 6:
strcpy(ch[q],"six hundred");
break;
case 7:
strcpy(ch[q],"seven hundred");
break;
case 8:
strcpy(ch[q],"eight hundred");
break;
case 9:
strcpy(ch[q],"nine hundred");
break;
}
q++;
}
string numberProcess(string number)
{
char s[20],s1[number.length()+1];
strcpy(s1, number.c_str());
int i,j,k=8,n,p=0,l=1,sefr=0, x[9];
int va2,va;
j=number.length()+1;
strcpy(s,s1);
for (i=1 ;i<=8;i++)
x[i]=0;
for (i=j-1;i>=0;i--)
{
n=s[i]-48;
if (l==1000)
{
l=1;
k--;
}
x[k]=x[k]+(n*l);
l=l*10;
}
for (i=1;i<=8;i++)
sefr=sefr+x[i];
if(sefr==0)
{
strcpy(ch[q],"zero");
q++;
}
else
{
for (i=8;i>=1;i--)
if (x[i]!=0)
{
va2=0;
for (va=8;va>=i+1;va--)
va2=va2+x[va];
if(va2 != 0)
{
strcpy(ch[q]," and ");
q++;
}
switch(i)
{
case 7:
strcpy(ch[q]," Thousand ");
break;
case 6:
strcpy(ch[q]," Million ");
break;
case 5:
strcpy(ch[q]," billion ");
break;
case 4:
strcpy(ch[q]," Trillions ");
break;
case 3:
strcpy(ch[q]," Quadrillion ");
break;
case 2:
strcpy(ch[q]," Quintilion ");
break;
}
q++;
if (x[i]<20)
first_digit(x[i]);
if ((x[i]>19)&&(x[i]<100))
second_digit(x[i]);
if (x[i]>99)
third_digit(x[i]);
}
return out;
}
for(int df = 100; df>=0;df--)
{
if(strcmp(ch[df],"" ))
strcat(out ,ch[df]);
}
}
int main(){
ifstream datas;
string line;
datas.open("da.txt",ios::out);
if(datas)
{
while(getline(datas,line))
{
cout<<numberProcess(line);
}
}
return 0;
}
This program works till getting a string, after that it does not encrypt it.
I am a beginner programmer who just started to code,kindly help me
I suspect,the logic part of the code is not working
{
void logic(int l,char a[],char b[],Rotor r1,Rotor r2,Rotor r3);
}
and for the cryptic text I used:https://nicerc.org/wp-content/uploads/2019/08/Enigma-Pringles-1.pdf
Expected output:
Enter the three Rotor arrangements(1/2/3):1 2 3
Enter the three Rotor position:a a a
Enter the String:r
The encrypted string is:n
Actual output:
Enter the three Rotor arrangements(1/2/3):1 2 3
Enter the three Rotor position:a a a
Enter the String:r
The encrypted string is:
Code:
#include<iostream>
using namespace std;
class Rotor
{
public:
char r[26][3];
Rotor(int m,char ch);
};
void movement(Rotor r1,Rotor r2,Rotor r3,int l)
{
//for rotor1
int k,i;
k=1;
for(i=0;i<26;i++)
{
if (i==25)
{
r1.r[i][1]=r1.r[0][1];
}
else
{
r1.r[i][1]=r1.r[i+1-k][1];
k=0;
}
//for rotor2
if(l%26==0 && l!=0)
{
for(i=0;i<26;i++)
{
if(i==25)
{
r2.r[i][1]=r2.r[0][1];
}
else
{
r2.r[i][1]=r2.r[i+1][1];
}
}
}
//for rotor3
if(l%676==0 && l!=0)
{
for(i=0;i<26;i++)
{
if(i==25)
{
r3.r[i][1]=r3.r[0][1];
}
else
{
r3.r[i][1]=r3.r[i+1][1];
}
}
}
}
}
void logic(int l,char a[],char b[],Rotor r1,Rotor r2,Rotor r3);
int main()
{
int m,n,o;
char ch1,ch2,ch3;
cout<<"Enter the three Rotor arrangements(1/2/3):";
cin>>m>>n>>o;
cout<<"Enter the three Rotor position:";
cin>>ch1>>ch2>>ch3;
Rotor r1(m,ch1);
Rotor r2(n,ch2);
Rotor r3(o,ch3);
int p;
if(m<n && n<o)
{
p=1;
}
else if(m<o && o<n)
{
p=2;
}
else if(n<m && m<o)
{
p=3;
}
else if(n<o && o<m)
{
p=4;
}
else if(o<n && n<m)
{
p=5;
}
else if(o<m && m<n)
{
p=6;
}
char a[500],b[500];
cout<<"Enter the String:";
cin>>a;
int l;
for(l=0;a[l]!='\0';l++)
{
if(p==1)
{
logic(l,a,b,r1,r2,r3);
movement(r1,r2,r3,l);
}
else if(p==2)
{
logic(l,a,b,r1,r3,r2);
movement(r1,r3,r2,l);
}
else if(p==3)
{
logic(l,a,b,r2,r1,r3);
movement(r2,r1,r3,l);
}
else if(p==4)
{
logic(l,a,b,r2,r3,r1);
movement(r2,r3,r1,l);
}
else if(p==5)
{
logic(l,a,b,r3,r2,r1);
movement(r3,r2,r1,l);
}
else if(p==6)
{
logic(l,a,b,r3,r1,r2);
movement(r3,r1,r2,l);
}
}
cout<<b;
return 0;
}
Rotor::Rotor(int m,char ch) //constructor
{
if(m==1)
{
char r[26][3]=
{
'a','c','b','d','c','e','d','a','e','b','f','g','g','i',
'h','f','i','h','j','l','k','j','l','m','m','o','n','k',
'o','r','p','n','q','t','r','u','s','p','t','q','u','w',
'v','s','w','v','x','z','y','x','z','y'
};
int i,j,d;
for(i=0;i<26;i++)
{
if(r[i][1]==ch)
break;
}
for(j=0;j<26;j++)
{
d=(j+i)%26;
r[j][1]=r[d][1];
}
}
else if(m==2)
{
char r[26][3]=
{
'a','b','b','a','c','d','d','e','e','f','f','c','g','i',
'h','g','i','h','j','l','k','m','l','j','m','k','n','o',
'o','n','p','q','q','r','r','p','s','u','t','s','u','t',
'v','y','w','z','x','v','y','w','z','x'
};
int i,j,d;
for(i=0;i<26;i++)
{
if(r[i][1]==ch)
break;
}
for(j=0;j<26;j++)
{
d=(j+i)%26;
r[j][1]=r[d][1];
}
}
else if(m==3)
{
char r[26][3]=
{
'a','d','b','a','c','b','d','g','e','h','f','c','g','e',
'h','f','i','j','j','i','k','l','l','k','m','p','n','m',
'o','n','p','o','q','s','r','t','s','q','t','r','u','x',
'v','y','w','z','x','u','y','v','z','w'
};
int i,j,d;
for(i=0;i<26;i++)
{
if(r[i][1]==ch)
break;
}
for(j=0;j<26;j++)
{
d=(j+i)%26;
r[j][1]=r[d][1];
}
}
}
void logic(int l,char a[],char b[],Rotor r1,Rotor r2,Rotor r3)
{
char temp=a[l];
switch(temp)//Rotor 1
{
case 'a':
temp=r1.r[0][1];
break;
case 'b':
temp=r1.r[1][1];
break;
case 'c':
temp=r1.r[2][1];
break;
case 'd':
temp=r1.r[3][1];
break;
case 'e':
temp=r1.r[4][1];
break;
case 'f':
temp=r1.r[5][1];
break;
case 'g':
temp=r1.r[6][1];
break;
case 'h':
temp=r1.r[7][1];
break;
case 'i':
temp=r1.r[8][1];
break;
case 'j':
temp=r1.r[9][1];
break;
case 'k':
temp=r1.r[10][1];
break;
case 'l':
temp=r1.r[11][1];
break;
case 'm':
temp=r1.r[12][1];
break;
case 'n':
temp=r1.r[13][1];
break;
case 'o':
temp=r1.r[14][1];
break;
case 'p':
temp=r1.r[15][1];
break;
case 'q':
temp=r1.r[16][1];
break;
case 'r':
temp=r1.r[17][1];
break;
case 's':
temp=r1.r[18][1];
break;
case 't':
temp=r1.r[19][1];
break;
case 'u':
temp=r1.r[20][1];
break;
case 'v':
temp=r1.r[21][1];
break;
case 'w':
temp=r1.r[22][1];
break;
case 'x':
temp=r1.r[23][1];
break;
case 'y':
temp=r1.r[24][1];
break;
case 'z':
temp=r1.r[25][1];
break;
}
switch(temp)//Rotor 2
{
case 'a':
temp=r2.r[0][1];
break;
case 'b':
temp=r2.r[1][1];
break;
case 'c':
temp=r2.r[2][1];
break;
case 'd':
temp=r2.r[3][1];
break;
case 'e':
temp=r2.r[4][1];
break;
case 'f':
temp=r2.r[5][1];
break;
case 'g':
temp=r2.r[6][1];
break;
case 'h':
temp=r2.r[7][1];
break;
case 'i':
temp=r2.r[8][1];
break;
case 'j':
temp=r2.r[9][1];
break;
case 'k':
temp=r2.r[10][1];
break;
case 'l':
temp=r2.r[11][1];
break;
case 'm':
temp=r2.r[12][1];
break;
case 'n':
temp=r2.r[13][1];
break;
case 'o':
temp=r2.r[14][1];
break;
case 'p':
temp=r2.r[15][1];
break;
case 'q':
temp=r2.r[16][1];
break;
case 'r':
temp=r2.r[17][1];
break;
case 's':
temp=r2.r[18][1];
break;
case 't':
temp=r2.r[19][1];
break;
case 'u':
temp=r2.r[20][1];
break;
case 'v':
temp=r2.r[21][1];
break;
case 'w':
temp=r2.r[22][1];
break;
case 'x':
temp=r2.r[23][1];
break;
case 'y':
temp=r2.r[24][1];
break;
case 'z':
temp=r2.r[25][1];
break;
}
switch(temp)//Rotor 3
{
case 'a':
temp=r3.r[0][1];
break;
case 'b':
temp=r3.r[1][1];
break;
case 'c':
temp=r3.r[2][1];
break;
case 'd':
temp=r3.r[3][1];
break;
case 'e':
temp=r3.r[4][1];
break;
case 'f':
temp=r3.r[5][1];
break;
case 'g':
temp=r3.r[6][1];
break;
case 'h':
temp=r3.r[7][1];
break;
case 'i':
temp=r3.r[8][1];
break;
case 'j':
temp=r3.r[9][1];
break;
case 'k':
temp=r3.r[10][1];
break;
case 'l':
temp=r3.r[11][1];
break;
case 'm':
temp=r3.r[12][1];
break;
case 'n':
temp=r3.r[13][1];
break;
case 'o':
temp=r3.r[14][1];
break;
case 'p':
temp=r3.r[15][1];
break;
case 'q':
temp=r3.r[16][1];
break;
case 'r':
temp=r3.r[17][1];
break;
case 's':
temp=r3.r[18][1];
break;
case 't':
temp=r3.r[19][1];
break;
case 'u':
temp=r3.r[20][1];
break;
case 'v':
temp=r3.r[21][1];
break;
case 'w':
temp=r3.r[22][1];
break;
case 'x':
temp=r3.r[23][1];
break;
case 'y':
temp=r3.r[24][1];
break;
case 'z':
temp=r3.r[25][1];
break;
}
//REFLECTOR
switch(temp)
{
case 'a':
temp='e';
break;
case 'b':
temp='d';
break;
case 'c':
temp='h';
break;
case 'd':
temp='b';
break;
case 'e':
temp='a';
break;
case 'f':
temp='i';
break;
case 'g':
temp='l';
break;
case 'h':
temp='c';
break;
case 'i':
temp='f';
break;
case 'j':
temp='m';
break;
case 'k':
temp='o';
break;
case 'l':
temp='g';
break;
case 'm':
temp='j';
break;
case 'n':
temp='s';
break;
case 'o':
temp='k';
break;
case 'p':
temp='r';
break;
case 'q':
temp='u';
break;
case 'r':
temp='p';
break;
case 's':
temp='n';
break;
case 't':
temp='w';
break;
case 'u':
temp='q';
break;
case 'v':
temp='y';
break;
case 'w':
temp='t';
break;
case 'x':
temp='z';
break;
case 'y':
temp='v';
break;
case 'z':
temp='x';
break;
}
switch(temp)//Rotor 3
{
case 'a':
temp=r3.r[0][1];
break;
case 'b':
temp=r3.r[1][1];
break;
case 'c':
temp=r3.r[2][1];
break;
case 'd':
temp=r3.r[3][1];
break;
case 'e':
temp=r3.r[4][1];
break;
case 'f':
temp=r3.r[5][1];
break;
case 'g':
temp=r3.r[6][1];
break;
case 'h':
temp=r3.r[7][1];
break;
case 'i':
temp=r3.r[8][1];
break;
case 'j':
temp=r3.r[9][1];
break;
case 'k':
temp=r3.r[10][1];
break;
case 'l':
temp=r3.r[11][1];
break;
case 'm':
temp=r3.r[12][1];
break;
case 'n':
temp=r3.r[13][1];
break;
case 'o':
temp=r3.r[14][1];
break;
case 'p':
temp=r3.r[15][1];
break;
case 'q':
temp=r3.r[16][1];
break;
case 'r':
temp=r3.r[17][1];
break;
case 's':
temp=r3.r[18][1];
break;
case 't':
temp=r3.r[19][1];
break;
case 'u':
temp=r3.r[20][1];
break;
case 'v':
temp=r3.r[21][1];
break;
case 'w':
temp=r3.r[22][1];
break;
case 'x':
temp=r3.r[23][1];
break;
case 'y':
temp=r3.r[24][1];
break;
case 'z':
temp=r3.r[25][1];
break;
}
switch(temp)//Rotor 2
{
case 'a':
temp=r2.r[0][1];
break;
case 'b':
temp=r2.r[1][1];
break;
case 'c':
temp=r2.r[2][1];
break;
case 'd':
temp=r2.r[3][1];
break;
case 'e':
temp=r2.r[4][1];
break;
case 'f':
temp=r2.r[5][1];
break;
case 'g':
temp=r2.r[6][1];
break;
case 'h':
temp=r2.r[7][1];
break;
case 'i':
temp=r2.r[8][1];
break;
case 'j':
temp=r2.r[9][1];
break;
case 'k':
temp=r2.r[10][1];
break;
case 'l':
temp=r2.r[11][1];
break;
case 'm':
temp=r2.r[12][1];
break;
case 'n':
temp=r2.r[13][1];
break;
case 'o':
temp=r2.r[14][1];
break;
case 'p':
temp=r2.r[15][1];
break;
case 'q':
temp=r2.r[16][1];
break;
case 'r':
temp=r2.r[17][1];
break;
case 's':
temp=r2.r[18][1];
break;
case 't':
temp=r2.r[19][1];
break;
case 'u':
temp=r2.r[20][1];
break;
case 'v':
temp=r2.r[21][1];
break;
case 'w':
temp=r2.r[22][1];
break;
case 'x':
temp=r2.r[23][1];
break;
case 'y':
temp=r2.r[24][1];
break;
case 'z':
temp=r2.r[25][1];
break;
}
switch(temp)//Rotor 1
{
case 'a':
temp=r1.r[0][1];
break;
case 'b':
temp=r1.r[1][1];
break;
case 'c':
temp=r1.r[2][1];
break;
case 'd':
temp=r1.r[3][1];
break;
case 'e':
temp=r1.r[4][1];
break;
case 'f':
temp=r1.r[5][1];
break;
case 'g':
temp=r1.r[6][1];
break;
case 'h':
temp=r1.r[7][1];
break;
case 'i':
temp=r1.r[8][1];
break;
case 'j':
temp=r1.r[9][1];
break;
case 'k':
temp=r1.r[10][1];
break;
case 'l':
temp=r1.r[11][1];
break;
case 'm':
temp=r1.r[12][1];
break;
case 'n':
temp=r1.r[13][1];
break;
case 'o':
temp=r1.r[14][1];
break;
case 'p':
temp=r1.r[15][1];
break;
case 'q':
temp=r1.r[16][1];
break;
case 'r':
temp=r1.r[17][1];
break;
case 's':
temp=r1.r[18][1];
break;
case 't':
temp=r1.r[19][1];
break;
case 'u':
temp=r1.r[20][1];
break;
case 'v':
temp=r1.r[21][1];
break;
case 'w':
temp=r1.r[22][1];
break;
case 'x':
temp=r1.r[23][1];
break;
case 'y':
temp=r1.r[24][1];
break;
case 'z':
temp=r1.r[25][1];
break;
}
temp=b[l];
}
After getting the program to work after an extensive disucssion, we were able to determine that the posted code contains the following bugs:
In the constructor Rotor::Rotor, a local variable r is declared, which then shadows the member variable r. This means that the constructor does not initialize its member variable in any way; it only initializes its local variable. This was not intended. Also, the array has 26 * 3 elements, which is 78. However, only 52 of these elements are being initialized.
As pointed out by someone else in the comments section, the line cout << b causes undefined behavior, because b is not initialized (and might not even contain a null terminating character).
In the function logic, the line temp=b[l]; should be b[l]=temp;.
The Rotor variables r1, r2, r3 are passed to the function movement by value, not by reference or pointer. That way, any changes to these variables will not affect the corresponding variables of the function main. However, it was intended that they should be affected. Therefore, they should be passed by reference or by pointer instead.
In contrast to the function movement, it is acceptable to pass the variables r1, r2 and r3 to the function logic by value, because the functions does not modify these variables. However, passing these variables by value is highly inefficient, since these variables each have a size of 78 bytes, so 234 bytes in total. That means that these 234 bytes must be copied every time the function logic is called. This copying would not be necessary if these variables were passed by reference or by pointer. Therefore, I would recommend passing these variables by const reference or const pointer instead. This still ensures that the original variables are not modified, while not requiring a copy to be made.
Also, the excessively long switch statements in the function logic are unnecessary. The first switch statement can be simplified to the following:
temp = r1.r[temp-'a'][1];
This trick only works if the characters are stored consecutively in the character set. This is the case with ASCII and most other character sets. However, I believe that the C++ standard itself does not require that the characters be stored consecutively in the character table. But virtually all platforms do this. So it is rather safe to use this trick.
Also, the "REFLECTOR" switch statement can be shortened to the following two lines:
const char reflector_map[] = "edhbailcfmogjskrupnwqytzvx";
temp = reflector_map[temp-'a'];
I want to move the position of symbol "A" in the terminal via the following code in c++, but the terminal closes and seems it does not enter the for loop. I don't know where I am wrong. I will be grateful if you help me:
'w' should move it up
's' should move it down
'a' and 'd' to right and left
#include <iostream>
#include <conio.h>
#include <string>
#include <Windows.h>
using namespace std;
void goToXY(int x=0,int y=0)
{
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(h,c);
}
int main()
{
char symbol='A';
int X=0, Y=0;
goToXY(X,Y);
cout<<symbol;
for(;;)
{
if(kbhit())
{
char ch = getch();
switch(ch)
{
case 'w':
goToXY(X,Y-1);
cout<<symbol;
case 's':
goToXY(X,Y+1);
cout<<symbol;
case 'a':
goToXY(X-1,Y);
cout<<symbol;
case 'd':
goToXY(X+1,Y);
cout<<symbol;
}
}
getch();
return 0;
}
}
1) You forgot to add break; after each case-body.
2) And you've put return 0; in the body of for-loop, so your program stops after first iteration.
Try this:
for(;;)
{
if(kbhit())
{
char ch = getch();
switch(ch)
{
case 'w':
goToXY(X,Y-1);
cout<<symbol;
break;
case 's':
goToXY(X,Y+1);
cout<<symbol;
break;
case 'a':
goToXY(X-1,Y);
cout<<symbol;
break;
case 'd':
goToXY(X+1,Y);
cout<<symbol;
break;
}
}
}
getch();
return 0;
You have not used the break; statement after each case in your switch statement. Hope this helps.
switch(ch)
{
case 'w':
goToXY(X,Y-1);
cout<<symbol;
break;
case 's':
goToXY(X,Y+1);
cout<<symbol;
break;
case 'a':
goToXY(X-1,Y);
cout<<symbol;
break;
case 'd':
goToXY(X+1,Y);
cout<<symbol;
break;
}
I am having trouble with this program. What it is intended to do is read a list of words, then take input for how long the word is, from the words of this length, count up the total of all the letters in them, sort them by highest frequency, and ask the user for the one with the highest frequency. Before it asks, it checks if that letter has already been asked be looping though the array prevguess. My problem is that, if I enter that 'yesletter' is true, the content of this array gets changed at the point after the inline comment "problem occurs here". The value of q within this test loop I put in doesn't change, but the value itself changes.
I know that the loop in main is infinite right now, but the program is not finished.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int i(0),n(0),counter(0),limit(0),length,mastercount(0);
int acount(0),bcount(0),ccount(0),dcount(0),ecount(0),fcount(0),gcount(0),hcount(0);
int icount(0),jcount(0),kcount(0),lcount(0),mcount(0),ncount(0),ocount(0),pcount(0);
int qcount(0),rcount(0),scount(0),tcount(0),ucount(0),vcount(0),wcount(0),xcount(0),ycount(0),zcount(0);
int letters[2][26];
char prevguess[26];
char words[60000][30];
void initarray() {
int len(0);
string line;
char temp;
ifstream wordlist ("words.txt");
if (wordlist.is_open())
{
while (wordlist.good())
{
getline (wordlist,line);
len=line.length();
for (n=0;n<30;n++)
{
if (n<len){
temp=line.at(n);
words[i][n]=temp;
}
else{
words[i][n]='*';
}
}
i++;
counter++;
}
}
else
{
cout<<"file not opened";
}
wordlist.close();
}
void selectlength()
{
int x(0),y(0);
bool shorter(false),longer(false);
cout <<"length of word"<<endl;
cin >> length;
limit=counter;
counter=0;
for (i=0;i<limit;i++){
shorter=false;
longer=false;
for (n=0;n<length;n++){
if (words[i][n]=='*')
{
shorter=true;
break;
}
}
if (words[i][length] != '*')
{
longer=true;
}
if (!longer && !shorter)
{
n=0;
for (y=0;y<30;y++)
{
if (n<length){
words[x][y]=words[i][n];
n++;
}
else{
words[x][y]='*';
}
}
x++;
counter++;
}
}
}
void mostletters(){
char temp;
for (i=0;i<counter;i++){
for (n=0;n<=length;n++){
temp=words[i][n];
switch (temp){
case 'a':
acount++;
break;
case 'b':
bcount++;
break;
case 'c':
ccount++;
break;
case 'd':
dcount++;
break;
case 'e':
ecount++;
break;
case 'f':
fcount++;
break;
case 'g':
gcount++;
break;
case 'h':
hcount++;
break;
case 'i':
icount++;
break;
case 'j':
jcount++;
break;
case 'k':
kcount++;
break;
case 'l':
lcount++;
break;
case 'm':
mcount++;
break;
case 'n':
ncount++;
break;
case 'o':
ocount++;
break;
case 'p':
pcount++;
break;
case 'q':
qcount++;
break;
case 'r':
rcount++;
break;
case 's':
scount++;
break;
case 't':
tcount++;
break;
case 'u':
ucount++;
break;
case 'v':
vcount++;
break;
case 'w':
wcount++;
break;
case 'x':
xcount++;
break;
case 'y':
ycount++;
break;
case 'z':
zcount++;
break;
}
}
}
}
void guessmost(){
int x,y,temp,temp2,q;
for (x=0;x<26;x++){
letters[0][x]=x;
switch (x){
case 0:
letters[1][x]=acount;
break;
case 1:
letters[1][x]=bcount;
break;
case 2:
letters[1][x]=ccount;
break;
case 3:
letters[1][x]=dcount;
break;
case 4:
letters[1][x]=ecount;
break;
case 5:
letters[1][x]=fcount;
break;
case 6:
letters[1][x]=gcount;
break;
case 7:
letters[1][x]=hcount;
break;
case 8:
letters[1][x]=icount;
break;
case 9:
letters[1][x]=jcount;
break;
case 10:
letters[1][x]=kcount;
break;
case 11:
letters[1][x]=lcount;
break;
case 12:
letters[1][x]=mcount;
break;
case 13:
letters[1][x]=ncount;
break;
case 14:
letters[1][x]=ocount;
break;
case 15:
letters[1][x]=pcount;
break;
case 16:
letters[1][x]=qcount;
break;
case 17:
letters[1][x]=rcount;
break;
case 18:
letters[1][x]=scount;
break;
case 19:
letters[1][x]=tcount;
break;
case 20:
letters[1][x]=ucount;
break;
case 21:
letters[1][x]=vcount;
break;
case 22:
letters[1][x]=wcount;
break;
case 23:
letters[1][x]=xcount;
break;
case 24:
letters[1][x]=ycount;
break;
case 25:
letters[1][x]=zcount;
break;
}
}
for (y=0;y<26;y++){
//problem occurs here (I think)
for (q=mastercount-1;q>=0;q--){
cout<<"for array index:"<<q;
cout << " the value of prevguess is "<<prevguess[q]<<endl;
}
for (x=26;x>=1;x--){
if (letters[1][x]>letters[1][x-1])
{
temp=letters[1][x-1];
letters[1][x-1]=letters[1][x];
letters[1][x]=temp;
temp2=letters[0][x-1];
letters[0][x-1]=letters[0][x];
letters[0][x]=temp2;
}
}
}
}
void letterguess(){
int x(0),z;
char guess;
bool goodletter(false),yesletter(false),alreadyguess(false);
while (!goodletter){
guess=letters[0][x]+97;
if (mastercount==0){
alreadyguess=false;
}
else{
for (z=mastercount-1;z>=0;z--){
if (guess==prevguess[z]){
alreadyguess=true;
}
}
}
if (!alreadyguess){
cout<<"is your letter "<< guess<<endl;
cin >> yesletter;
prevguess[mastercount]=guess;
}
if (yesletter && !alreadyguess){
goodletter=true;
}
else {
cout<<"wrong"<<endl;
x++;
}
mastercount++;
if (mastercount>26){
break;
}
}
}
int main() {
bool found(false);
initarray();
selectlength();
while (!found){
mostletters();
guessmost();
letterguess();
if (mastercount>26){
break;
}
}
}
I believe the problems start a bit lower than your comment.
for (x=26;x>=1;x--){
if (letters[1][x]>letters[1][x-1])
When x is 26, letters[1][x] will be out of range.