very simple encoder

ok so im trying to create a simple encoder that will simply set letters equal to numbers. i figure that i will have to make a char variable for every letter but do i have to make an integer for every char variable as well? or can the computer recognize an input letter as a number as well? ooor am i going about this alll wrong and i need an array and/or strings more than i need char and int variables?
See you can try dis :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
  int i=0;
  char str[50];

  cout<<"Enter the string to be encoded :";
  cin>>str;
  cout<<"The encoded string is :";
 while(str[i]!='\0')
 {
   cout<<(int)str[i];
   i++;
 }

 _getch();
}
ok so in yours you are using strings and arrays so i was wrong in thinking i should make variables for everything. right?
WOAH! that was sweet but it didnt quite agree with my computer. it kinda looks like it kept encoding past my input string. should i put in a statement that tells it not to encode past '.' or something?

oh i missed the void main() but now that i have it it wont compile. it says
void function returning a value
Last edited on
Your getting an error because void main is wrong. main must return an int, so use int main. Also, he is using C strings, which will cause you much grief if your not careful. I would suggest using std::string instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;

    cout << "Type a string: ";
    getline(cin, str);

    cout << "Your string reversed is: "
    for (int i = str.size()-1; i>=0; i--)
    {
        cout << str[i];
    }

    return 0;
}


Reference: http://cplusplus.com/reference/string/string/
Last edited on
whats the difference between std::strings and C strings? arent they both strings?
now it says i have an assertion problem.
Last edited on
@ModShop : Nice :)
sorry that was unclear it says

Debug Assertion Failed

Expression: string subscript out of range
You have a downward loop but you're increasing your counter.

You also aren't printing the 0 character.

1
2
3
4
5
// bad
for (unsigned int i = str.size()-1; i>0; i++)  // >, ++

// good
for (int i = str.size()-1; i>=0; i--)  // signed, >=, -- 
Last edited on
whats the difference between std::strings and C strings? arent they both strings?

C strings require a lot more labor to make them do what you want... kind of like C.

And you don't have to type out everything/make variables for everything, that would just be a pain in the ass. Since your computer probably uses ASCII (assuming it was made in the last decade), every character is already mapped to an integer for you. You just have to print them out as integers as xordux showed you. If you want it to be a little less obvious (anyone with half a brain could crack it in about zero seconds) you can run it through an algorithm.
Last edited on
Disch wrote:
You have a downward loop but you're increasing your counter.

You also aren't printing the 0 character.


My bad! This is what results from chronic sleep deprivation and writing tons of upward incrementing for loops before hand.
im getting still getting the same failure for both of dirsch's statements.

thanks for the explanation ascii that helps.

also, in modshop's code, how come you dont have to create int i before putting it into parameters? and also just to make sure im understanding this properly you are setting i equal to str right? and thats so that you can manipulate it like a regular integer?
Last edited on
Not quite. You only have create variables outside of a loop in C. In C++, you can actually create them outside of the for loop, and then there scope ends with the loop. Secondly, he is not setting i to str, he is setting i to the length of str. He subtracts 1 to ignore the delimiter character '\0'. Because he now has the indexes of each character, he can access them all individually and do whatever he wants to them. In this case he chose to reverse them. If he wanted to print them out encrypted as integers as you wanted too he could change the body of the for loop to:
 
std::cout << static_cast<int>(str[i]) << std::endl;
Last edited on
OH! thats pretty frickin sweet! thanks alright ill work with that new knowledge and see what comes back.
ok so now i fully understand that code but still when i put it in i still get the "assertion failure" i mentioned before. what is that and if anyone has encountered it before what should i do?
Please show us all your code, it's impossible to for us to figure it out otherwise.
Last edited on
sure sorry.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;

    cout << "Enter string: ";
    getline(cin, str);

    cout << "\n\nRESULT:"
    for (signed int i = str.size()-1; i>=0; i--)
    {
        cout << str[i];
    }

    return 0;
}


its really just ModShop's code i just didnt fully understand how it worked until now.
No need to apologize :)
Try changing the condition in your for loop to:
 
i > 0

Also, at the moment it's just outputting the string exactly as it started. If you want to some kind of encryption run each character through a function or something before you output it.
Topic archived. No new replies allowed.