why doesn't this program work?

I built this program but the decoder does not work properly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

using namespace std;

int main(void) {
char user[100],ch;
int counter=0,cntr;
int i = 0;

cout<<"Enter Word\nNo Spaces! \n> ";
cin >> user;

cout<<"Enter move count\n1-25 only\n> ";
cin >>counter;
cntr=counter;

while ( user[i] != '\0'){
ch = user[i];
if ( ch >= 97)
user[i] = (user[i] + counter - 97) % 26 + 97;
else user[i] = (user[i] + counter - 65) % 26 + 65;
i++;}

cout<<endl<<user<<" <Here is the encoded output\n\n";
counter=26-cntr;

ch = user[i];
if ( ch >= 97)
user[i] = (user[i] + counter - 97) % 26 + 97;
else user[i] = (user[i] + counter - 65) % 26 + 65;
cout<<endl<<user<<" <Here is the decoded output\n\n";
}
closed account (28poGNh0)
what do expecte as a result ?

please exemples
the origonal text typed in
closed account (28poGNh0)
You can simply use two arrays one for the original word the second for the coded word
I don't know how to use arrays, I only added the decoder onto the program. I am still new at c++
Your variable char user[100] , is an array. If you don't know how to use them, look it up, Google it, or there is a great resource on this website.
ok bu it still doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;

int main(void) {
char user[100],ch,userout[100];
int counter=0,cntr;
int i;

cout<<"Enter Word\nNo Spaces! \n> ";
cin >> user;

cout<<"Enter move count\n1-25 only\n> ";
cin >>counter;
cntr=counter;

while ( user[i] != '\0'){
ch = user[i];
if ( ch >= 97)
user[i] = (user[i] + counter - 97) % 26 + 97;
else user[i] = (user[i] + counter - 65) % 26 + 65;
i++;}

cout<<endl<<user<<" <Here is the encoded output\n\n";
counter=26-cntr;
userout[i]=user[i];
while ( user[i] != '\0'){
ch = user[i];
if ( ch >= 97)
userout[i] = (user[i] + counter - 97) % 26 + 97;
else userout[i] = (user[i] + counter - 65) % 26 + 65;
i++;}
cout<<endl<<userout<<" <Here is the decoded output\n\n";
}

it crashes after this:
userout[i]=user[i];
Last edited on
instead of a while loop, try a for loop like
for(int i = 0;i >= user.size(); i++)
Line 14: error: request for member 'size' in 'user', which is of non-class type 'char [100]'
Line 23: error: request for member 'size' in 'user', which is of non-class type 'char [100]'
Last edited on
Use the size of the array instead of "user.size()" as arrays don't have size() methods. Also you will want strictly less than rather than greater than or equal to.
for(int i = 0; i < 100; ++i)
I've reverted back to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;

int main(void) {
char user[100],ch,userout[100];
int counter=0,cntr;
int i,a;
cout<<"Enter Word\nNo Spaces! \n> ";
cin >> user;
cout<<"Enter move count\n1-25 only\n> ";
cin >>counter;
cntr=counter;
while ( user[i] != '\0'){
ch = user[i];
if ( ch >= 97)
user[i] = (user[i] + counter - 97) % 26 + 97;
else user[i] = (user[i] + counter - 65) % 26 + 65;}
cout<<endl<<user<<" <Here is the encoded output\n\n";
counter=26-cntr;
userout[a]=user[i]; ///Crashes Here and I don't know why.
while ( userout[a] != '\0'){
ch = userout[a];
if ( ch >= 97)
userout[a] = (user[a] + counter - 97) % 26 + 97;
else userout[a] = (user[a] + counter - 65) % 26 + 65;}
cout<<endl<<userout<<" <Here is the decoded output\n\n";
}
anyone here?
@jasonwynn10

I hadn't really looked at your program earlier, so never commented on it. Anyway, it intrigued me, so I fiddled with it a bit. Here's what I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
using namespace std;

int main() 
{
 char user[100] = {' '},ch,userout[100] = {' '};
 int counter=0,cntr;
 int i=0,a=0;
 cout<<"Enter Word\nNo Spaces! \n> ";
 cin >> user;
 cout<<"Enter move count\n1-25 only\n> ";
 cin >>counter;
 cntr=counter;
 do
 {
	ch = user[i];
	if ( ch >= 97)
	 user[i] = (user[i] + counter - 97) % 26 + 97;
	else
	 user[i] = (user[i] + counter - 65) % 26 + 65;
	i++;
 } while ( user[i] != '\0');
 cout<<endl <<user <<" <Here is the encoded output\n\n";
 counter=26-cntr;

do
{
	ch = user[a];
	if ( ch >= 97)
	 userout[a] = (user[a] + counter - 97) % 26 + 97;
	else 
   userout[a] = (user[a] + counter - 65) % 26 + 65;
a++;
}while ( user[a] != '\0');
 cout<<endl<<userout<<" <Here is the decoded output\n\n";
return 0;
}

I changed your while's, to a do/while to stop the userout value having a smiley face symbol at the end. Anyway, I hope this is what you were wanting as results in your program.
Last edited on
thanks, now all I need to do is be able to do this with full sentences and output it into an encoded .txt file and a decoced .txt file.
how can I get this to work with sentences? I also need to get a proper text output to the file
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
///#include <string>
#include <fstream>
using namespace std;

int main()
{
 char user[100] = {' '},ch,userout[100] = {' '};
 int counter=0,cntr;
 int i=0,a=0;
 cout<<"Enter Word\nNo Spaces! \n> ";
 cin >> user;
 cout<<"Enter move count\n1-25 only\n> ";
 cin >>counter;
 cntr=counter;
 do
 {
	ch = user[i];
	if ( ch >= 97)
	 user[i] = (user[i] + counter - 97) % 26 + 97;
	else
	 user[i] = (user[i] + counter - 65) % 26 + 65;
	i++;
 } while ( user[i] != '\0');
 cout<<endl <<user <<" <Here is the encoded output\n\n";
 counter=26-cntr;
 ofstream myfile;
  myfile.open ("encoded.txt");
  myfile << user[i];
  myfile.close();

do
{
	ch = user[a];
	if ( ch >= 97)
	 userout[a] = (user[a] + counter - 97) % 26 + 97;
	else
   userout[a] = (user[a] + counter - 65) % 26 + 65;
a++;
}while ( user[a] != '\0');
 cout<<endl<<userout<<" <Here is the decoded output\n\n";
  myfile.open ("decoded.txt");
  myfile << userout[a];
  myfile.close();
return 0;
}
Last edited on
@jasonwynn10

Sorry, I won't be able to help out.
Last edited on
Topic archived. No new replies allowed.