Hi, I'm having some trouble with a program I'm trying to make for a uni assignment (first year so basic stuff).
The encrypting method used is that each digit in the given number is replaced by ((the sum of that digit plus seven) modulo 10) then the first and last “encrypted” digits are swapped.
I'm getting some errors though and I'd appreciate it if someone would be able to help me out, I've tried Googling and re-reading my code but can't seem to identify the problem.
I know that I'd be able to tidy this up using local variables rather than creating new ones during the process.
/* 3-digit number encryption program */
#include <iostream>
usingnamespace std;
int Digit1;
int Digit2;
int Digit3;
int NewDigit1;
int NewDigit2;
int NewDigit3;
int FinalDigit1;
int FinalDigit2;
int FinalDigit3;
int originalNumber;
int EncryptedNumber;
int main()
{
cout << "Enter the original three-digit number:";
cin >> originalNumber;
void isolateDigits();
void replaceDigits();
void swapDigit1WithDigit3();
void recomposeEncryptedNumber();
cout << "The Encrypted Number is:" ;EncryptedNumber;
return 0;
}
{
{
/*find out the 3 digits that make up the number*/
void isolateDigits()
Digit1 = (originalNumber / 100);
Digit2 = ((originalNumber % 100) 10);
Digit3 = (originalNumber % 10);
}
{
{
/*”encrypt” each of the three digits*/
void replaceDigits()
NewDigit1 = ((Digit1 + 7) % 10);
NewDigit2 = ((Digit1 + 7) % 10);
NewDigit3 = ((Digit1 + 7) % 10);
}
}
{
{
/*swap the first and last digit*/
void swapDigit1WithDigit3()
FinalDigit1 = NewDigit3;
FinalDigit2 = NewDigit2;
FinalDigit3 = NewDigit1;
}
}
{
{
/*recompose encrypted number from new values*/
void recomposeEncryptedNumber()
EncryptedNumber = (((FinalDigit1 * 100) + (FinalDigit2 * 10) + (FinalDigit3));
}
}
}
Errors;
1 2 3 4 5 6 7 8 9
1 - error C2447: '{' : missing function header (old-style formal list?) - line 32
2 - IntelliSense: expected a declaration - line 32
3 - IntelliSense: expected a '{' - line 38
4 - IntelliSense: expected a declaration - line 44
5 - IntelliSense: expected a '{' - line 50
6 - IntelliSense: expected a declaration - line 54
7 - IntelliSense: expected a '{' - line 61
8 - IntelliSense: expected a declaration - line 66
9 - IntelliSense: expected a "{" - line 73
Thanks in advance, you'd be doing me a massive favour helping me out here!
I don't usually bump topics on forums but this program has to be done by tomorrow morning 9am (currently 1:42am) and I hope to get a few hours sleep beforehand haha!
It's probably something obvious that should be staring me in the face but I am completely unaware of what's wrong with the above code...
You've misplaced a bunch of brackets.
Brackets come after the name of a function, and at the end of a function.
1 2 3 4 5 6 7 8 9 10 11 12 13
{ //<---- why is this here?
{ //<---- why is this here?
/*find out the 3 digits that make up the number*/
void isolateDigits()
// { <--- there should be a bracket here
Digit1 = (originalNumber / 100);
Digit2 = ((originalNumber % 100) 10);
Digit3 = (originalNumber % 10);
}
{ //<--- why is this here?
Functions have the syntax of
1 2 3 4
type foobar()
{ //open bracket
foo + bar //logic
} //close bracket
go back through your code and find where you left brackets out, and where you put brackets in for no reason.
Hope you get this thing working by the morning!
-Thumper
It's now compiled with no errors, but whenever I try to use the program, all it will return is
"The encrypted number is: 0Press any key to continue . . ."
/* 3-digit number encryption program */
#include <iostream>
usingnamespace std;
int Digit1;
int Digit2;
int Digit3;
int NewDigit1;
int NewDigit2;
int NewDigit3;
int FinalDigit1;
int FinalDigit2;
int FinalDigit3;
int originalNumber;
int EncryptedNumber;
int main()
{
cout << "Enter the original three-digit number:";
cin >> originalNumber;
void isolateDigits();
void replaceDigits();
void swapDigit1WithDigit3();
void recomposeEncryptedNumber();
cout << "The encrypted number is: "
<< EncryptedNumber;
return 0;
}
/*find out the 3 digits that make up the number*/
void isolateDigits()
{
Digit1 = (originalNumber / 100);
Digit2 = ((originalNumber % 100) * 10);
Digit3 = (originalNumber % 10);
}
/*”encrypt” each of the three digits*/
void replaceDigits()
{
NewDigit1 = ((Digit1 + 7) % 10);
NewDigit2 = ((Digit1 + 7) % 10);
NewDigit3 = ((Digit1 + 7) % 10);
}
/*swap the first and last digit*/
void swapDigit1WithDigit3()
{
FinalDigit1 = NewDigit3;
FinalDigit2 = NewDigit2;
FinalDigit3 = NewDigit1;
}
/*recompose encrypted number from new values*/
void recomposeEncryptedNumber()
{
EncryptedNumber = (((FinalDigit1 * 100) + (FinalDigit2 * 10) + (FinalDigit3)));
}
Woops, i missed that too.
The compiler reads your file linearly, so when there's no mention of isolateDigits before it's called, it doesn't know what you're referring to. So when you define functions underneath the main function, you need to prototype them
1 2 3 4 5 6 7 8 9
void isolateDigits(); //these are prototypes.
void replaceDigits();
void swapDigit1WithDigit3();
void recomposeEncryptedNumber();
int main()
{
//code
}
When your compiler reaches the call of isolateDigits() in main, it realizes that it's been mentioned before, and knows what to look for.
Just one very final touch now is that I need leading zeroes on the output - i.e. the output "20" will be represented as "020". Any easy way to do this?
Before outputting EncyptedNumber, test to see if it's less than 100. If so, manually output a "0" before the number.
The same applies if it's less than 10, output two zeroes.
You are correct. My example would only work for one trailing zero.
I left the situation of two trailing zeroes out under the assumption you'd fill it in :P
The logic is the same. If < then 10, and > than 0, add two zeroes, then output EncryptedNumber.
I re-read your post again, and have now edited mine and feel like an idiot for even asking the question haha.
Been in the library for over six hours now doing various assignments and revising - time well spent but my cognitive functioning is now suffering haha!
int main()
{
cout << "Enter the original three-digit number:";
cin >> originalNumber;
isolateDigits();
replaceDigits();
swapDigit1WithDigit3();
recomposeEncryptedNumber();
cout << "The encrypted number is ";
if(EncryptedNumber < 100 || EncryptedNumber >= 10)
cout << "0" << EncryptedNumber << endl;
if(EncryptedNumber < 10 || EncryptedNumber >= 0)
cout << "00" << EncryptedNumber << endl;
cout << "\n";
return (0);
And my output from the program reads (user input is the 333 which when encrypted should result in 000)
1 2 3 4 5
Enter the original three-digit number:333
The encrypted number is 00
000
Press any key to continue . . .
How can I get it to just display the "000"
The answer for such a simple problem must be staring me in the face but my lack of sleep is punishing me severely.
You need use AND comparison instead of OR. You want both conditions to be true in order to determine whether to put one or two zeroes up. (that was originally my bad. It's pretty late at night for me too, so i'm also having trouble thinking. Lol.)
1 2 3 4 5 6 7
cout << "The encrypted number is ";
if(EncryptedNumber < 100 && EncryptedNumber >= 10)
cout << "0" << EncryptedNumber << endl;
elseif(EncryptedNumber < 10 && EncryptedNumber >= 0)
cout << "00" << EncryptedNumber << endl;
else
cout << "Encrypted number > 999, or < 0. (out of bounds)" << endl;
how do i fix the error with the open bracket above the line starting with char?
#include <iostream>
using namespace std;
void menu(int a, int b, int c, int d, char symbol);
float numerator,denominator;
void addFractions(int a,int b, int c, int d, int &numerator, int &denominator);
void subtractFractions(int a, int b, int c, int d, int &numerator, int &denominator);
void multiplyFractions (int a, int b, int c, int d, int &numerator, int &denominator);
void divideFractions (int a, int b, int c, int d, int &numerator, int &denominator);
int main()
{
int a, b, c, d;
cout << "first, we will need your numerators/denominators." << endl;
cout << "What is your first fractions numerator?" << endl;
cin>> a;
cout << "What is your first fractions denominator?" << endl;
cin >> b;
cout << "What is your second fractions numerator?" << endl;
cin >> c;
cout << "What is your second fractions denominator?" << endl;
cin >> d;
}
void menu(int a, int b, int c, int d, char symbol)
{
cout << "choose one of the following: + (addition), - " <<
"(subtraction), * (multiplication), and finally / (division)." << endl;
cin >> symbol;
cout << endl;
}
void addFractions (int a, int b, int c, int d, int &numerator, int &denominator)
{
numerator = (a*d) + (b*c);
denominator = (b*d);
}
void subtractFractions (int a, int b, int c, int d, int &numerator, int &denominator)
{
numerator = (a*d)-(b*c);
denominator = (b*d);
}
void multiplyFractions(int a, int b, int c, int d, int &numerator, int&denominator)
{
numerator = (a*c);
denominator = (b*d);
}
void divideFractions(int a, int b, int c, int d, int &numerator, int &denominator)
{
numerator = (a*d);
denominator = (b*c);
}