About Use of #include Preprocessor Directive in C++ - c++

I stumble on the following compilation error in C++ with g++ compiler:
error on line line 1 with message:
invalid preprocessing directive #a
(with a caret above the character a) which is followed by another,probably consequent, error on line 4 with message:
cout was not declared in this scope.
The editor i am using is Code blocks 10.05 with mingw.I tried removing .h extension from the iostream file include statement;switching among different File encoding options;and replacing the angular bracket with single quotes and double quotes as well.i am stuck on it.Pardon if it is a duplicate(although i went through several already asked questions in relevance).
The following code illustrates the problem:
#‪include ‬<iostream.h>
int main()
{
cout<< "abc"+8;
cout<< "def"+4;
cout<< "ha";
return 0;
}

cout exists within the namespace std
So either
#‪include‬<iostream>
//...
std::cout << "abc" << 8;
//...
or
#‪include‬<iostream>
using namespace std;
//...
or
#‪include‬<iostream>
using std::cout;
//...
I tend to prefer the 1st if I'm only using it once or twice, The second if I'm using a lot of different pieces from a namespace (and only in a cpp file), or the third if I'm only using a piece or 2 from a namespace but using the same couple many times.
Additionally as stated in the comments, don't use the 2nd one in headers. See: "using namespace" in c++ headers
Also, you have an invalid character in your #include. You can see it in a hex editor or Note how stackoverflow doesn't highlight them the same:
#‪include‬<iostream>
#include<iostream>
Fully working code:
#include<iostream>
using std::cout;
int main()
{
cout << "abc" << 8;
cout << "def" << 4;
cout << "ha";
return 0;
}
Produces the following output abc8def4ha after I corrected for trying to add 8 to a char*

You have to use std::cout, which means that the "cout" keyword is part of the standard library.

The "invalid directive" error is caused by some invisible Unicode characters in the #include directive; perhaps you copied this from a website that embedded some formatting characters in the code. They can be seen in the question, if you look at the source in a hex editor. That error should be fixed by deleting and retyping the #include line.
You'll probably have other errors, since the code is fifteen years out of date; most modern compilers don't provide pre-standard libraries. These days, the standard library headers don't have a .h extension:
#include <iostream>
and nearly all the names they declare are scoped inside the std namespace:
std::cout << "ha";
Finally, "abc"+8 doesn't do anything sensible. The string literal is an array of four characters, and +8 tries to give you a pointer to the ninth character, which doesn't exist. The result is undefined behaviour.
If you want to print "abc" followed by "8", then you want:
std::cout << "abc" << 8;

Try using it like this:-
#‪include ‬<iostream>
using std :: cout;
cout is the part of std library

If you've got caret above a, try retyping your #include.
You might accidentally type alternative i which looks similar but has different code.
Suggestions about std:: are only relevant for the second error you're getting.
I also didn't fully understand what you were trying to achieve with "abc"+8.

Related

How do I get the string data type to work in a class? [duplicate]

This question already has answers here:
String Undeclared In C++
(5 answers)
Closed 1 year ago.
I am new to C++ and am currently working on a project that wants me to turn a Roman Numeral into a Hindu-Arabic number (our normal number system). I'm currently writing a class RomanNumeralType which needs to store a roman numeral type string. When I try to run the code below I get error code C3646: unknown override specifier and also error codes C4430 and C2061 which both address string (in bold) not being a valid identifier. It was my impression that #include fixed this however I am unsure why it is not working. I currently do not have a function that actually converts the numeral to a number but I want to fix this problem first. How do I get the string data type to work in my class?
Here is a minimally reproduced example of my code
//main
#include <iostream>
#include <iomanip>
#include "Header2.h"
#include <string>
using namespace std;
int main() {
string ans;
cout << "Please enter a string of Roman Numerals (M,D,C,L,X,V,I): " << endl;
cin >> ans;
}
RomanNumeralType r1(ans);
cout << "Your original Roman Numeral was: " << r1.getNumeral() << endl;
cout << "Your Roman Numeral as a integer is: " << r1.getNumber(r1.getNumeral()) << endl;
return 0;
}
//Header2.h
#ifndef HEADER2
#define HEADER2
#include <string>
class RomanNumeralType {
public:
**string** romanNumeral;
RomanNumeralType(**string** x)
{romanNumeral = x;}
**string** getNumeral()
{return romanNumeral;}
}
When you do #include <string> think of it as though you are pasting in the contents of a file shared across all developers that has all the definitions for string. That class lives within the std namespace, so when you want to use a string, you have to tell the compiler that string is in std. The preferred method is to use the fully qualified name, so every time you use string, you would replace it with std::string.
The other option is to do what you did in main.cpp, using namespace std;This tells the compiler that the namespace to look in is in std. This is generally considered bad practice for actual programs, but it is OK if it is a small, one off program like you seem to be writing.
If you do the first option, you will have to change everything in the std namespace such as cout, but it is clearer that you didn't write your own.

decltype was not declared in this scope

I am trying to make a program that reads strings and tells me how many punctuation marks are in it. However, when I try to compile it, it gives
the error, "'decltype' was not declared in this scope'. I have just started
c++ in the last month and am new to its concepts.
I'm using Dev C++ 5.11 as the IDE for the code. The code is from the book
c++ Primer fifth edition on page 92
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main() {
string s("Hello World!!!");
decltype(s.size() punct_cnt = 0;
// count the number of punctuation characters in s
for (auto c : s) // for every char in s
if (ispunct(c)) // if the character is punctuation
++punct_cnt;
cout << punct_cnt << " punctuation characters in " << s << endl;
}
I expect it to give the output 3, but it gives the error message, "'decltype' was not declared in this scope'.
decltype was only introduced in C++11, and presumably DevC++ is not instructing the compiler to use c++11 mode. In your compiler options, instruct DevC++ to pass the following command-line flag:
-std=c++11

I am attempting to write my first "Hello World" code in Visual Studio 2013. Why am I received a "IntelliSense: no operator message" & "error C2563"?

#include <istream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main() {
count <<"Hello World! I am C++ Program." <<endl;
return 0;
}
IntelliSense: no operator message Line 7, Column 8
error C2563:mismatch in formal parameter list Line 7, Column 1
Replace #include <istream> with the correct header, #include <iostream>.
Helpful mnemonic:
io = input/output
stream = "stream of data"
Additionally, the name of the standard output stream is std::cout or cout with the std:: namespace scope removed.
Helpful mnemonic:
std:: = Standard library's
cout = console output
The problems you are having with your simple block of code is simply: spelling errors.
Firstly, you have misspelled the input/output stream file in your include statement, so you need to rename the header file to:
#include <iostream>
There is no heade file named istream.
Secondly, you also misspelled the cout function to count. Change that line to:
cout << "Hello World! I am C++ Program." << endl;
Those lines should work now.
Also a recommendation for your future programs; avoid using the line
using namespace std;
Why? Because as you move on to more complex programming, you will undoubtedly learn and begin to define a data type or variable, and sometimes, that name may also be used by the standard library. As a result, you will have a hard time trying to differentiate the variables or data types you defined and the ones defined in the std library.
Therefore, try and attach std:: before every function that is a part of the standard library.
EDIT:
The code you posted in the comments box is pretty unreadable, so I just fixed it and have posted it below:
#include <iostream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main()
{
cout <<"Hello World! I am C++ Program." <<endl;
return 0;
}
I've tried this in my IDE and fixed with the same and only recommendations from above. It works for me.

Having problems compiling my first C++ program

I tried compiling the following program on XCode on my Mac, and I get these errors:
*Non-ASCII charactes are not allowed outside of literals and identifiers. Fix it: Delete ""
*Use of undeclared identifier 'Hello'
#include <iostream>
using namespace std;
int main()
{
cout << “Hello there world!”;
return 0;
}
This program is literally verbatim from the textbook, "A First Book of C++: An Introduction to Programming" so I'm not sure why it would not work. Is this a Mac vs. PC issue?
The "pretty quotes" copied from your textbook are not valid characters.
Change:
cout << “Hello there world!”;
// ^ ^ These characters are not correct.
To:
cout << "Hello there world!";
The editor you use to type code must not be one that replaces the characters you type with characters that might look nicer.
It seems that you are using non-ASCII double quotes
try with this string (cut and paste) "Hello there world", note the different leading apostrophes.
Try using the Code::Blocks compiler
http://www.codeblocks.org/

How do I use quotation marks in fstream?

I want to output a line into a .plt file that says "one-D Hydro" with the double quotation marks and so far I have this problem.
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
fstream gnuplot_file;
gnuplot_file.open ("sod.plt");
gnuplot_file<<"set title"<< ""one-D Hydro""<<std::endl;
gnuplot_file.close();
system("gnuplot.exe sod.plt");
return 0;
}
Line 11 will not allow it to compile because I can't seem to close the statement. The error is just as useless by the way.
gnuplot_call.cpp|11|error: expected ';' before 'one'|
With C++03 (or even C) use backslashes to escapes double-quotes in string literals:
gnuplot_file << "set title" << "\"one-D Hydro\"" << std::endl;
Notice that gnuplot may require you to also escape some characters, e.g. if you wanted the title to contain quotes!
With C++11 you could use raw string literals, e.g.
gnuplot_file<< R"*(set title "one-D Hydro")*" << std::endl;
BTW, you could be interested by popen(3) and pclose, if your operating system and C++ library provides them. You would just popen the gnuplot process and send commands to it, finally pclose-ing it.
Try to include escape character [i.e.,back slash] in the code where you are trying to add double quotes.
For example:
"\"one-D Hydro\""
btw why are you using std:: once you have defined namespace for it you can directly use endl.