Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been working through the "Programming: Principles and Practice using C++" book, and this example was meant to illustrate how data can be lost in type conversions. But when I tried to execute it, it keeps telling me 'Main' : must return a value. I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".
I've copied the code from the book to Visual Studio and everything else I've copied has worked as expected - would anyone be able to tell me how I could fix this please? I don't know why it's happening with this specific example or how to make it stop asking for a return value.
Sorry if this is dumb, I'm very inexperienced and I've tried googling for an answer, but suggested solutions like "return 0" don't work :)!
#include "std_lib_facilities.h"
int main()
{
double d = 0;
while (cin >> d)
{
int i = d;
char c = i;
int i2 = c;
cout << "d==" << d
<< " i==" << i
<< " i2==" << i2
<< " char(" << c << ")\n";
}
}
I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".
That's a separate, unrelated problem uncovered by fixing the first one. Add the return, then deal with the unresolved externals error.
If that's your whole program, you're unresolved external is most likely iostream. You need to include that and use the correct namespace.
Consider the following code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Better yet, forgo the using statement and use std::cout so that you don't have to worry about namespace collision. See this question Why is “using namespace std;” considered bad practice?
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Related
This question already has answers here:
source is compiled without proper #include
(2 answers)
Why does this C++ code compile with some compilers but not others?
(4 answers)
C++ "size_t" doesn't need "cstddef" header?
(5 answers)
Deciding which standard header files to #include
(3 answers)
C++ code compiles without include
(2 answers)
Closed 10 months ago.
I am learning programming in c++ and using vs code editor. Many times I read in tutorials that a particular function of stl is of this header file. For example toupper() is in the header<cctype>. Hence, when I have to use that particular function, I should include that header file in the source code. But, I have multiple times that even without including a proper header file of any function, the function works properly and gives output for some functions while for others it doesn't. For example: in the case of toupper() and putchar() it works, but in the case of sqrt() and many others, it doesn't.
#include<iostream>
#include<cmath>
int main() {
int a, b;
std::string c;
char ch;
a = 25;
b = 21;
c = "bk";
ch = 'y';
std::cout << "maximum of a and b is " << std::max (a,b) << std::endl;
std::cout << "minimum of a and b is " << std::min(a, b) << std::endl;
std::cout << "square root of a is " << std::sqrt(a) << std::endl;
std::cout << "length of string stored in c is " << c.length() << std::endl;
std::cout << "Upper of char stored in ch is " << char(toupper(ch)) << std::endl;
putchar(toupper(ch));
return 0;
}
I tried to find the process using ctrl + F12 and I saw what it looked that putchar() or toupper() was getting included implicitly from the compiler. If so then why not for functions like sqrt().
And also follow on question:
Since I am a beginner learning C++, hence I don't want implicit inclusion, can I force vs code for explicit inclusion of header files and run only functions inside explicitly included header files?
A detailed explanation will be appreciated. 🙏
In MSVC, the string header contains:
// The <cctype> include below is to workaround many projects that assumed
// <string> includes it. We workaround it instead of fixing all the upstream
// projects because <cctype> is inexpensive. See VSO-663136.
#include <cctype>
So cctype is included from string in your case.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following code is not working in DEV C++:
#include <iostream>
using namespace std;
void main()
{
cout << "Hello world";
}
How can I do this?
Also, tell me if I can get almost all the functionality of Turbo C++ in Dev C++. Also, tell me if I can easily switch to DEV C++ if I know a moderate amount in Turbo C++. I just want to use C++ with Python 3.x for console applications including GUI.
From what I see in your initial question (printf works while ostream doesn't) the problem may be in buffering. Try that:
std::cout << "Hello world" << std::endl;
or, if you wish to avoid a newline:
std::cout << "Hello world";
std::cout.flush();
There is a difference between streams and printf: the streams are not outputing the data immediately but buffer that to optimize the performance. This however means that in some environments for the small outputs like yours it may stuck forever waiting for more output.
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello world";
return 0;
}
I used int main instead of void main.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This problem seems to only be present on MacOS, compilation is fine on linux also using clang.
the following code is a simplification but demonstrates the issue,
#include<iostream>
int index = 0;
int main()
{
std::cout << index << std::endl;
}
throws this error on compilation:
main.cpp:2:5: error: redefinition of 'index' as different kind of symbol
int index = 0;
^
/usr/include/strings.h:73:7: note: previous definition is here
char *index(const char *, int) __POSIX_C_DEPRECATED(200112L);
^
main.cpp:5:18: warning: address of function 'index' will always evaluate to
'true' [-Wpointer-bool-conversion]
std::cout << index << std::endl;
~~ ^~~~~
main.cpp:5:18: note: prefix with the address-of operator to silence this warning
std::cout << index << std::endl;
^
&
1 warning and 1 error generated.
These were the compiler arguments used:
clang++ -std=c++11 main.cpp -o test
when removing iostream with stdio or nothing the code compiles as expected. Is their a way to fix this or will I have to rename my variable to avoid this?
I did find this but I am already using the C++11 flag and the -std=c11 flag doesn't seem to be valid for C++ code.
The specific version of clang/xcode you're using happens to include the <strings.h> header when you include <iostream>. <strings.h> provides a function called index() at global scope. Thus, you cannot declare a variable also at global scope with the same name.
Either rename the variable, or move it into main():
#include <iostream>
int main()
{
int index = 0;
std::cout << index << std::endl;
}
This works because when a variable has the same identifier as something else but is in a different scope, it is considered a different entity altogether.
To give you an example on how that works, consider this code:
#include <iostream>
int myVar = 0;
int main()
{
int myVar = 1;
std::cout << myVar << '\n';
std::cout << ::myVar << '\n';
}
This will print:
1
0
because myVar refers to the local variable, but ::myVar to the one at global scope.
Is their a way to fix this or will I have to rename my variable to avoid this?
C++ provides namespaces specifically to avoid collisions between names. You can create one for your variable:
#include<iostream>
namespace MyGlobals {
int index = 0;
}
int main()
{
std::cout << MyGlobals::index << std::endl;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am having trouble with printing a string in C++.
I know there are lots of topics about this matter on SO, but most say to include <string>, <iostream> or namespace std. But I did all of that but still encounter the issue. Here is my code and the error.
#include <iostream>
#include <string>
using namespace std;
//...
void affiche_date(int annee, int nbjours) {
string mois;
if (nbjours>31) {
mois = "avril";
nbjours -= 31;
} else {
mois = "avril";
}
cout << "Date de Paques en " << annee << " : " << nbjours << " " << mois << end;
}
int main() {
int annee ( demander_annee() ) ;
int jour ( date_paques(annee) );
affiche_date(annee, jour);
}
Here is the error I get when I compile:
"error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)"
This error is coming from the line with the cout in the function I gave you.
I am using Geany on linux Ubuntu and using c++11.
Thanks for you help
std::end() is a function for getting an iterator to the end of a container.
You meant to use the std::endl stream manipulator instead.
Note: avoid using namespace std; in your actual code, either take advantage of using directives to bring in only what you need, or favor qualifying names with their namespaces, like I have here.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am getting the error "Error: 'cout' was not declared in this scope," but I included <iostream>, which was the solution that was given for similar problems in my research. My code is this:
#include <iostream>
int main(){
Sally so;
Cout << "omg wtf is this on my shoe" << endl;
}
Its cout not Cout, notice the case difference.
cout is in the namespace std. In order to use it you need to resolve the namespace with std::, so use std::cout << ....
As much as people will tell you to just do using namespace std, dont. For more info, see Why is “using namespace std” considered bad practice?.
You are writing "Cout" not "cout" - C++ is case sensitive, so those two are not the same thing.
You should write std::cout since the cout stream lives in the std namespace.
The same goes for endl which should be std::endl.
You could avoid writing std:: by using using namespace std; but I wouldn't advice it - it pulls all of the namespace into the current scope which may not hurt for a trivial program will bite for a more complex one (at the very least, don't do it in headers).
Just do this:
#include <iostream>
int main(){
Sally so;
std::cout << "omg wtf is this on my shoe" << std::endl;
}
Btw; unless you know you want to flush the stream, prefer '\n' over std::endl.
Change your code to this:
#include<iostream>
using namespace std;
int main()
{
Sally so;
cout<<"the text"<<endl;
}
Hope it helps!!
Cheers