C++ is not right

Pages: 12
HI People,
I am just learning C++. I have a couple of books that I am learning from.
I think there is something wrong with Visual Studio 2019 Pro.
I write programs and they have errors all of the time except for the simplest of programs. I get squiggly line under things like cin and cout.
Thanks for your help
Mark

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()
{
    int integer1, integer2, sum;

    cout << "Enter first integer\n";

    cin >>  integer1;

     cout <<  "Enter second integer \n";

    
    cin >> integer2;

    sum = integer1 + integer2;

     cout  <<  "Sum is " << sum << endl;

    return 0;

}

Last edited on
What books are you using?
char cout << "Enter first integer\n";
this should be: std::cout << "Enter first integer\n";
the same for the other lines. Put std:: brefore cout and cin not the type of a variable.
you need to qualify them:
std::cin >> integer1;

you can also place using statements at the top of the program:
using std::cin;
then it will work without the std:: down in the code.

Older c++ compilers did not require the qualification and old code, old books do not have it.
Last edited on
You are using wrong syntax.
With cout or cin, you should not write int or char etc.
You should write std:: with every cout or cin or endl etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
int main()
{
    int integer1, integer2, sum;

    std::cout << "Enter first integer\n";

    std::cin >>  integer1;

    std::cout <<  "Enter second integer \n";

    
    std::cin >> integer2;

    sum = integer1 + integer2;

    std::cout  <<  "Sum is " << sum << std::endl;

    return 0;

}


If you don't want to write std:: every time.

You can add
using namespace std
at the start of program to get rid of this problem, just like below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
    int integer1, integer2, sum;

    cout << "Enter first integer\n";

    cin >>  integer1;

    cout <<  "Enter second integer \n";

    
    cin >> integer2;

    sum = integer1 + integer2;

    cout  <<  "Sum is " << sum << endl;

    return 0;

}


But if you have more than one namespace in program, you should avoid using namespace. And consider to write std:: with every command.
Last edited on
C++ is not right

With modern C++ you have to specify the namespace. It's std::cin / std::cout.

There's an online C++ tutorial here, though it is not up-to-date.
C++ Language Tutorial - http://www.cplusplus.com/doc/tutorial/

Another online C++ tutorial that is kept up-to date:
Learn C ++ - https://www.learncpp.com/

I can't recommend Learn C++ too much.

One thing you need to realize when using Visual Studio 2019 is the C++ version defaults to C++14, manually changeable to C++17 and later. That likely means nothing to you at this time, but can have a great impact when you try out code samples you see around the internet.

In a nutshell C++ as a language changed a lot with C++11. C++14 made some changes, C++17 made a lot more changes. C++20 will add even more significant changes when officially released AND the various compilers implement the changes.

VS 2019 is a decent compiler and IDE (integrated development environment), but it is not one that makes learning the language easy for a beginner. All the bells and whistles can be overwhelming at the start.
I am using using namespace std;
I did the corrections that somebody sent me and I am still getting errors. What's up with that?
I am even getting squiggly lines under the words The temperature
Last edited on
Just show the new code to us. We can't help you if you just say "squiggly line" (if you hover over the squiggle or look at your Error List panel, you should see more detailed information).
The books I am using are Learn C++ quickly. Hands on C++ game animation programming Beginning C++ game programming. None of these books are very good.
Books are outdated even before they are printed. Online tutorials are hit and miss about being up-to-date.

There's an online C++ tutorial here, though it is not up-to-date.
C++ Language Tutorial - http://www.cplusplus.com/doc/tutorial/

Another online C++ tutorial that is kept up-to date:
Learn C ++ - https://www.learncpp.com/

Learn C++ is one of the best English based online tutorials IMO. You can learn at your own pace, either doing each chapter one after the other, or "bounce around" from the site index.

My programming book collection is over 40 printed and bound books, the ones that I have kept because even outdated they have some good information between the covers.

From "straight" C/C++ console mode to Windows GUI app creation, console-based game creation to using DirectX. All years out of date.

There are specialty eBooks I've purchased that cover modern C++ features in my collection as well. They are not "teach me programming from scratch" type books. The authors presume the reader already has a decent grasp of core features of C/C++.

The eBooks cover select C++11, 14 and 17 features, they are not a complete guide.

When C++20 is officially released AND compiler creators add the new features will it be another round of "teach me as if I were a 3 year-old." Self-teaching oneself C++ can be fun and frustrating.
Squiggly lines sounds like a spell checker. If it is then ignore it or turn it off.
I don't think it makes a lot of sense to focus on learning the absolute bleeding edge of the language. Each version is more or less a strict superset of the previous version, so even if you learn something somewhat outdated you'll still be able to practice and later on move on to newer versions without much difficulty. As long as the book isn't absolute dogshit you'll be fine.
#include pch.h
#include <iostream>
#include math.h

all of the includes have squiggly lines

using namespace std;
int main()
{
float temp_f{}, temp_c{};

cout << “Enter temperature in Fahrenheit” << endl; Enter temperature has squiggly lines

cin >> temp_f;

temp_c = (temp_f – 32) * (5.0 / 9.0);

cout << “The temperature in Celsius is” << temp_c << :\n”; The temperature has the same problem
}
Try this one. All of my programs have squiggly lines.

Here are the errors.

Severity Code Description Project File Line Suppression State
Error (active) E0013 expected a file name prog. 12 C:\VS Projects\prog. 12\prog. 12.cpp 3


Severity Code Description Project File Line Suppression State
Error (active) E0013 expected a file name prog. 12 C:\VS Projects\prog. 12\prog. 12.cpp 5

Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "“Enter" is undefined prog. 12 C:\VS Projects\prog. 12\prog. 12.cpp 11


Severity Code Description Project File Line Suppression State
Error (active) E0065 expected a ';' prog. 12 C:\VS Projects\prog. 12\prog. 12.cpp 11


Severity Code Description Project File Line Suppression State
Error (active) E0018 expected a ')' prog. 12 C:\VS Projects\prog. 12\prog. 12.cpp 15


This should give you an idea of what I am dealing with.
Thanks for the help.

Helios I accidentally bought 2019 pro.
againtry,
It is the spell check that is causing the errors.
Furry Guy. I used a book that I bought when I was in college back in the mid 90's. I wrote a program from that book and got the same problems. It was a simple cin cout program. I have many books on software, like you.
Last edited on
I don't think it makes a lot of sense to focus on learning the absolute bleeding edge of the language.

Teaching decades old language constructs to beginners as the only TRUE WAY -- C string vs. std::string, regular arrays vs. std::vector -- is not the way to go either.

Expose a beginner to some of the more basic parts of modern C++ first and then follow up with alternates.

Teaching what I consider to be outmoded lessons of the BASICS is not helpful.

We can argue and debate the specifics of the best way to teach the language, but too often the curriculum and lesson plans too many beginners are exposed to is woefully inadequate. They get frustrated they don't know about language features we take for granted like std::string or std::vector.

One of my biggest pet peeves is using namespace std;. ARGH! That should never be taught to a beginner, ever. :Þ
Does it matter whether I use a x86 or 64 bit?
x86 includes 64 bit. Not sure what you are looking at or clicking. There is hardware (most modern is 64 bit, unless special purpose, modern meaning < 5 years old) and most OS are 64 bit (but you can put an old 32 bit os on a 64 bit machine). So most likely you want to use 64 bit settings, unless you are using an old OS or old hardware or a 32 bit phone/tablet etc?
It depends. Is your CPU 32-bit (x86) or 64-bit (x64)? A 64-bit CPU can run both x86 and x64 compiled programs. A 32-bit CPU will execute only x86 programs.

There are memory storage differences between 32-bit and 64-bit for some of the common data types available in the language. With properly written code it should not matter.

Knowing the differences between the two is something you should be aware of, though. At this point in your learning I wouldn't consider it important. Especially to get all worked up about.

My development computer is Windows 10 Pro, x64 i5-2400. For the most part I compile as x86 only. Very rarely do I bother to compile as x64 or both. Only when I want to make sure my code is generic enough for the CPU "bitness."

Visual Studio can compile 64-bit apps with a 32-bit CPU, they just won't run.
Does it matter whether I use a x86 or 64 bit?

Not at this stage.

The program you shared contains many typos. The machine cannot process instructions that are full of spelling errors. Think of your code like a math formula, or part of a mathematical proof. Small mistakes render the whole thing nonsense.

In this case, you've forgotten to double-quote math.h and pch.h, and you've written a colon where a double quote is required, in the line
cout << “The temperature in Celsius is” << temp_c << :\n”;

There are other issues that aren't simple misspellings.

The minus sign in your program, , is not a minus sign but rather a character named U+2013 : EN DASH. This character cannot appear in C++ programs. You are required to use -, U+002D : HYPHEN-MINUS instead.

Similarly, the double quote character used in your program, , is called U+201C : LEFT DOUBLE QUOTATION MARK. This character can't appear in C++ programs. Straight quotes, " , called U+0022 : QUOTATION MARK must be used instead.

When this problem occurs, it's usually for one of two reasons:
1. You're copying your code from somewhere that uses funky characters (e.g., out of an e-book or a website) instead of typing it yourself; or
2. You're editing code using an unsuitable program (e.g., Microsoft Word or Libreoffice).

The solution is to (re-)write the code you want in a plain-text editor. VS 2019 contains an editor for this purpose.

Here is your code with the issues corrected:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "pch.h"
#include <iostream>

using namespace std;

int main()
{
    float temp_f{}, temp_c{};    
    cout << "Enter temperature in Fahrenheit" << endl;
    cin >> temp_f;
    temp_c = (temp_f - 32) * (5.0 / 9.0);
    cout << "The temperature in Celsius is " << temp_c << "\n"; 
}
Last edited on
Pages: 12