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
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required to perform setprecision stream manipulator
usingnamespace 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
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
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.
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.)
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.