Help please!

I have to make a program for school to encrypt a message with the "code Cesar". I made a function to encrypt the message but when I add the key to the message I want to have a caracter beteen 32 and 126.
To do that i put a if like:
message [i]= message [i]+key // test with key=45
if (message[i] > 126)
{
message [i] = 31 + (message [i]% 126);
}
else;

but when I run the prograam it never goes into the if.
Is it possible to have some help from one of you. It's very important for me!
Thank you!
What is a type of the message variable?
it is a string variable
if you post your code, then we can help you better. It is hard to diagnose your issues with the little information you give us. You will get more questions than answers if you dont.
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string cryptage (string message, int clef )
{
for (int i=0; i < message.size() ; i++) // on calcule les cases jusqua la taille du message
{
message [i] += clef; // on additionne la clef au caractere afin de crypter et on affecte cette valeur a message i

if (message[i] > 126) // si le message est plus grand que le caractere 126 max alors il faut le ramener a quelque chose de identique compris entre 32 et 126
{
message [i] = 31 + (message [i]% 126);
}
else;

}
return message ;

}

int main()
{
string message ;
int clef=0; // initialisation a zero afin de faire une clef qui sera égal a la somme des caracteres du code
string code;
ifstream msg ("msg.txt");
if (msg.is_open())
{
while (msg.good())
{
getline ( msg , message ); // envoi le texte du fchier dans la variable sting message
cout << message << endl;
}
msg.close();
}
cout << "entrer un mot de passe afin de crypter votre message" << endl;
cin >> code ;
for (int i =0; i < code.size(); i++)
{
clef += code [i];
}
cout << "la clef est " << clef << endl;
cout << "le message fcrypte avec votre clef est " << endl;
cout << cryptage (message,clef) << endl; // affichage du texte crypté a l'ecran
ofstream message_code ("message_code.txt", ios::trunc); // declaration du fichier dans lequel il faut ecrire le texte codé
if (message_code.is_open())
{
message_code << cryptage (message,clef) << endl;
message_code.close();
}
else
cout << "erreur avec le fichier" << endl;

return 0;
}

Here is my complete program.
there is only one problem with it. it doesn't do the if in the fonction "string cryptage".
Thank you for your help!
and how did you for the function that hold the caracters between 32 and 126? because it doesn't work in my program...
The variable message[i] is signed char, so if you assign:
message[i]=128;
it will contain -127.
I put an absolute value in the if condition and nomit works without any problem.
Thank you very much!
Topic archived. No new replies allowed.