digit extraction

Pages: 12
I keep getting an error message, error C2143: syntax error : missing ';' before '<<'

Can someone look at my coding and tell me what is wrong.

// Exercise 4.17: Encryption.cpp
// Encrypts data given by user.

#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required to perform setprecision stream manipulator

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution
int main()
{
int main=0;
int digit_one; // store digit one
int digit_two; // store digit two
int digit_three; // store digit three
int digit_four; // store digit four

cout <<"Enter four_digit number"; // asking the user to enter four_digit number
cin >>main; // four_digit number

digit_one=main%10; // digit_one is first four_digit number
digit_two=main/10%10; // digit_two is second four_digit number
digit_three=main/100%10; // digit_three is third four_digit number
digit_four=main/1000%10; // digit_four is fourth four_digit number

digit_one=(digit_one+7%10); // adds digit_one to 7 and mod 10
digit_two=(digit_two+7%10); // adds digit_two to 7 and mod 10
digit_three=(digit_three+7%10); // adds digit_three to 7 and mod 10
digit_four=(digit_four+7%10); // adds digit_four to 7 and mod 10

cout <<"Encrypted digits:"; << digit_three; digit_four; digit_one; digit_two; << endl;// display encrypted numbers


return 0; // indicate that program ended successfully

} // end function main

Thanks
Take a look at line 28...
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> // required to perform C++ stream I/O 
#include <iomanip> // required to perform setprecision stream manipulator

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution
int main()
{ 
	int main=0;
	int digit_one; // store digit one
	int digit_two; // store digit two
	int digit_three; // store digit three
	int digit_four; // store digit four
	
	cout <<"Enter four_digit number"; // asking the user to enter four_digit number
	cin >>main; // four_digit number
	
	digit_one=main%10; // digit_one is first four_digit number
	digit_two=main/10%10; // digit_two is second four_digit number
	digit_three=main/100%10; // digit_three is third four_digit number
	digit_four=main/1000%10; // digit_four is fourth four_digit number
	
	digit_one=(digit_one+7%10); // adds digit_one to 7 and mod 10
	digit_two=(digit_two+7%10); // adds digit_two to 7 and mod 10
	digit_three=(digit_three+7%10); // adds digit_three to 7 and mod 10
	digit_four=(digit_four+7%10); // adds digit_four to 7 and mod 10
	
	cout <<"Encrypted digits:"; << digit_three; digit_four; digit_one; digit_two; << endl;// display encrypted numbers
	
	
	return 0; // indicate that program ended successfully

} // end function main 


Semicolons are used to denote the end of a statement. So you could:

1) remove all semicolons except the last one (endl;), and place the << operator in the correct places
or
2) make this line several statements like:
cout <<"Encrypted digits:"; cout << digit_three; cout << digit_four; cout << digit_one; cout << digit_two; cout << endl;// display encrypted numbers

IMHO: option 2 makes the code hard to read, and option 1 is a very easy fix
Last edited on
could you explain 1, I tried to fix it the way you said but still not working.

Thanks
I tried to fix it the way you said but still not working.

That just means you did NOT do what he said.
Sorry, I am just a beginner, this is the first time I have taken this class, so I am still learning how to code, just very confused with this error and how to fix it correctly. I did look at that webpage but still confused, sorry.

1) remove all semicolons except the last one (endl;), and place the << operator in the correct places. Do I put the operator in front of
all the digits -------cout <<"Encrypted digits:"; << digit_three; digit_four; digit_one; digit_two; << endl;// display encrypted numbers

The semicolons are still all there.
cout is an output stream. When you send things to this output stream, it gets displayed on the "console", a.k.a. your screen. So code like:

cout << "Hello, World!";

would send the text

Hello, World!

to your screen. Your prompt would then immediately follow the exclamation point. To make the prompt appear on the next line (which makes more sense), you need to send an "endl" also known as the "end of line". So changing from

cout << "Hello, World!";

to

cout << "Hello, World!" << endl;

sends the text "Hello, World!" to the console followed by an "end of line" that puts your cursor on the next line, all the way to the left.

You can chain several things together, and let the output stream handle displaying it. Such as:

int comp_age = 5; // how old your computer is...
int your_age = 21; // how old you are
cout << "Hello, maria536. I am guessing you are " << your_age << " and your computer is " << comp_age << " years old." << endl;

would send the following to your screen. It would look like:

Hello, maria536. I am guessing you are 21 and your computer is 5 years old.

I hope this gets you closer to your goal.


My computer is 6 years old, what does the age of the computer have to do with anything? When you're not programming really fancy multimedia applications, you don't really need a new machine (though I do have a newer notebook too for playing games - but I found my old machine to be mostly more reliable than my old one, and that's even though it's already physically falling apart.)
cout <<"Encrypted digits:"; << "digit_three" <<"digit_four" <<"digit_one" <<" digit_two" << endl; // display encrypted numbers

So it should look like this
There's still a semicolon after "Encrypted digits".
ok I took that out, but it is not displaying the way it is supposed to, its supposed to encrypt like this when it prints out:

Enter four-digit number: 1234

Encrypted digits: 0 1 8 9

press any key to continue_
And what does it do instead?
Enter four-digit number:1234

Encrypted digits:digit_threedigit_fourdigit_onedigit_two

press any key to continue_

Sorry I am a beginner, this is the first time I have taken this class, I have been on this assaignment all day.

Thanks for you help
In my example above, I did the following:

1
2
3
int comp_age = 5; // how old your computer is...
int your_age = 21; // how old you are
cout << "Hello, maria536. I am guessing you are " << your_age << " and your computer is " << comp_age << " years old." << endl;


Notice that we are using 2 variables here, comp_age and your_age. If you send those to cout, it will print the numbers. If you surround the variable names with double quotes (i.e. "your_age" instead of your_age) you will get the text "your_age" on the screen instead of the value of "21". So shed your double quotes around your variables.
Remove the "'s around the variable names, that makes them be interpreted as string literals. Seriously, do you not have an IDE for stuff like that?

Also, if you want to have spaces in between the numbers you need to insert " " between the variable values.
Ok I did that---cout <<"Encrypted digits:"; << "digit_three" <<"digit_four" <<"digit_one" <<" digit_two" << endl; // display encrypted numbers

but it is still not printing the way it is supposed to.
You did what? I don't know what you did but it's not what I told you.
I took the semi-colon out

cout <<"Encrypted digits:" << "digit_three" <<"digit_four" <<"digit_one" <<" digit_two" << endl; // display encrypted numbers
No, see, you have quotes around your variable names. That's telling the compiler that you want to print the text inside your quotes.

1
2
3
int myvar = 5;
cout << myvar;  // prints "5"  (the contents of myvar)
cout << "myvar"; // prints "myvar" -- the actual string literal 
Pages: 12