Having problems compiling my first C++ program - c++

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/

Related

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

About Use of #include Preprocessor Directive in 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.

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.

C++ Program to execute another Program with Command line arguments

How do you execute a command line program with arguments from a c++ program? This is what I found online:
http://www.cplusplus.com/forum/general/15794/
std::stringstream stream;
stream <<"program.exe "<<cusip;
system(stream.str().c_str());
But it doesn't seem to accept an actual program location, so I am not sure how to apply this. My hope was to have something like this:
std::stringstream stream;
stream <<"C:\Tests\SO Question\bin\Release\HelloWorld.exe "<<"myargument";
system(stream.str().c_str());
This gives several warnings related to the backslashes - and the program does not work. Is it expecting you to have the program in some specific location?
This is the output I get in the console:
'C:\Tests' is not recognized as an internal or external command,
operable program or batch file.
ADDENDUM:
So based on Jon's answer the correct version for me looks like this:
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstring>
int main(int argc, char *argv[])
{
std::stringstream stream;
stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
<< " " // don't forget a space between the path and the arguments
<< "myargument";
system(stream.str().c_str());
return 0;
}
First of all, you should use double backslashes in literal strings whenever you want a single backslash to appear in the actual string value. This is according to the language grammar; a conforming compiler could do worse than simply warning about this.
In any case, the problem you are experiencing is due to the fact that paths containing spaces must be enclosed in double quotes in Windows. Since the double quotes themselves need to be escaped inside a C++ string literal, what you need to write is
stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
<< " " // don't forget a space between the path and the arguments
<< "myargument";
This gives several warnings related to the backslashes
I believe \ is an escape character in C++ using \\ instead will probably solve this problem.

What would 'std:;' do in c++?

I was recently modifying some code, and found a pre-existing bug on one line within a function:
std:;string x = y;
This code still compiles and has been working as expected.
The string definition works because this file is using namespace std;, so the std:: was unnecessary in the first place.
The question is, why is std:; compiling and what, if anything, is it doing?
std: its a label, usable as a target for goto.
As pointed by #Adam Rosenfield in a comment, it is a legal label name.
C++03 §6.1/1:
Labels have their own name space and do not interfere with other identifiers.
It's a label, followed by an empty statement, followed by the declaration of a string x.
Its a label which is followed by the string
(expression)std: (end of expression); (another expression)string x = y;
The compiler tells you what is going on:
#include <iostream>
using namespace std;
int main() {
std:;cout << "Hello!" << std::endl;
}
Both gcc and clang give a pretty clear warning:
std.cpp:4:3: warning: unused label 'std' [-Wunused-label]
std:;cout << "Hello!" << std::endl;
^~~~
1 warning generated.
The take away from this story: always compile your code with warnings enabled (e.g. -Wall).