This question already has answers here:
Error in strtok split using c++
(3 answers)
Closed 2 years ago.
I am practicing some C++ but i am having trouble splitting the user input. I am using eclipse ide 2020-03, with mingw32-gcc-g++-bin 9.2.0-1. below is my code,
#include <iostream>
#include <string>
using namespace std;
int main() {
string orderbook_symbol[500000][8]; //orderid, ordertime, symbol, buy/sell, qty, price, exp time, strike
string user_order;
char * pch;
string done= "done trading";
while(user_order.compare(done) != 0) {
cin >> user_order;
pch = strtok(user_order," ");
}
}
and when i hit compile i see this error:
23:22:06 **** Incremental Build of configuration Debug for project stasd ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\stasd.o" "..\\src\\stasd.cpp"
..\src\stasd.cpp: In function 'int main()':
..\src\stasd.cpp:26:9: error: 'strtok' was not declared in this scope; did you mean 'strtol'?
26 | pch = strtok(user_order," ");
| ^~~~~~
| strtol
23:22:06 Build Failed. 1 errors, 0 warnings. (took 560ms)
I took the example here: http://www.cplusplus.com/reference/cstring/strtok/
and i don't understand why a function call is getting not in scope error.
You're including <string>, whereas strtok is part of <string.h> or <cstring>.
See
include string or string.h
for the differences between these.
Related
This question already has answers here:
create many cpp files in one project [closed]
(4 answers)
Closed 7 years ago.
I have 2 files and i want to compile and run them using make command. I created a Makefile named "Makefile". They are compiled but shows an error
all: hello
hello: pgm1.o pgm2.o
g++ pgm1.o pgm2.o -o hello
pgm1.o: pgm1.cpp
g++ -c pgm1.cpp
pgm2.o: pgm2.cpp
g++ -c pgm2.cpp
They are compiled but shows an error
make -f Makefile
g++ pgm1.o pgm2.o -o hello
pgm2.o: In function `print2()':
pgm2.cpp:(.text+0x0): multiple definition of `print2()'
pgm1.o:pgm1.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [hello] Error 1
pgm1.cpp
#include <iostream>
#include "pgm2.cpp"
using namespace std;
int main()
{
cout<<"Thiss is program 1";
print2();
return 0;
}
<>pgm2.cpp
#include <iostream>
using namespace std;
void print2()
{
cout<<"Thiss is program 2";
}
What is that error? How can i rectify it?
You are compiling both these files into singular output file but your pgm1.cpp already contains the function print2() by virtue of the line #include "pgm2.cpp"...
Possible solutions can be:
1) Remove the include file and instead add a function declaration.
void print2();
2) As already pointed out create a header file and use include it instead of a .cpp file.
This question already has answers here:
printf with std::string?
(9 answers)
Closed 7 years ago.
I was reading this. That question contains following program.
#include <iostream>
#include <cstdio>
#include <string>
int main()
{
using namespace std;
string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString);
cin.get();
return 0;
}
I tried it on g++ 4.8.1 & it fails in compilation. g++ 4.8.1 gives following diagnosis.
9 47 [Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'
9 47 [Warning] format '%s' expects argument of type 'char*', but argument 2 has type 'std::string {aka std::basic_string<char>}' [-Wformat=]
What does this error mean? Should this program compile successfully or not? Which compiler is correct (g++ or MSVS 2010) ? Why MSVS 2010 accepts this code? Is the code invokes undefined behavior when compiled on MSVS 2010?
Surprising: I tried it on ideone which uses g++ 5.0 & surprisingly it compiles & runs fine. (See live demo here.). g++ 5.2.0 gives warning when I compile this code. (See live demo here). Why it compiles fine on ideone but fails on g++ 4.8.1? g++ 4.8.2(gives same diagnosis as g++ 4.8.1, 4.9.0,4.9.1,4.9.2 (gives error not warning). g++ 5.1.0 gives warning but program still compiles & runs fine.
Why different versions of g++ behave differently when compiling above program? Is this bug in g++ ? Clang also rejects this code to compile in response to answer given by #TemplateRex
Clang errors out with "error: cannot pass non-trivial object of type 'string' (aka 'basic_string') to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]" and suggests the fix "note: did you mean to call the c_str() method?"
#include <iostream>
#include <cstdio>
#include <string>
int main()
{
using namespace std;
string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString.c_str());
cin.get();
}
and it seems to work.
I've got some code that I'm running on Mac OS X that can't be compiled on the Virtual Machine running Linux Mint. This is a simple example. When I run it in Mac, all is fine, but I'm getting issues when I run the same code on Linux, so I'm assuming the library I'm including is not there, but should I be getting an include error then?
Here's the example code that runs on Mac.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
for (int i = 0; i < 10; i++){
string test = to_string(i);
cout << test << endl;
}
cout << "done" << endl;
return 0;
}
I get no issues here but running on Linux Mint, I get this when I try to compile:
for.cpp: In function 'int main()':
for.cpp:7:28 error: 'to_string' was not declared in this scope
string test = to_string(i);
^
make: *** [for] Error 1
Am I missing something? Any help would be much appreciated!
edit
I realize I forgot to include <string> on here and I fixed it, but what I changed (<string> included) still doesn't compile on Linux. I've used to_string before. I know that much in C++. I also tried adding <cstdlib>. Once again, this DOES compile on Mac and DOES NOT compile on Linux.
Here is my OSX output:
0
1
2
3
4
5
6
7
8
9
done
Here is my output on Linux Mint (Once again, Virtual Box, g++ make):
test.cpp: In function ‘int main()’:
test.cpp:9:28: error: ‘to_string’ was not declared in this scope
string test = to_string(i);
^
make: *** [test] Error 1
You could reproduce the problem yourself if you don't believe me. It's the same code, you can see for yourself if you want.
Compile your for.cpp file like this:
g++ -std=c++11 for.cpp
and run it with:
./a.out
The support for the to_string function in the <string> header was added in the C++11 version of the language, so you need to tell GCC to use that version. You can use the c++0x flag too, for example:
g++ -std=c++0x for.cpp
And you don't have to worry about <cstdlib>, that has nothing to do with it...
to_string() is defined in <string> if you are compiling with C++11 (but is not defined, or unreliably defined as an extension feature, if you are compiling with an earlier version of C++).
Reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string
SOLUTION:
I found a better solution. For some reason, I've read stdlib.h will not work on some linux systems. I used a different function to convert int to string.
on linux:
#include <stdio.h>
and then
for (int i = 0; i < 10; i++){
char buffer[10];
sprintf(buffer,"%d",i);
string stringInt = buffer;
cout << stringInt << endl;
// do whatever you want with the string
}
edit
To the person that down voted my solution to this, here's a post from six years ago basically saying the same thing.
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 8 years ago.
Improve this question
So the objective of this program is to display the initials of a name typed in by the user. So if I type James Issac Newton, it should give me JIN. I tried compiling my code in the terminal using g++ -Wall -o name name.cpp but it wouldn't compile. What exactly am I doing wrong??
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
char name[100];
gets(name);
char a,b,c;
cin>>name;
a=name[0];
int x;
for (int i=0;i<=strlen(name);i++)
{
if (name[i]==" ")
{
b=name[i+1];
x=i;
break;
}
}
for (int j=x;j<=strlen(name);j++)
{
if (name[j]==" ")
{
c=name[j+1];
}
}
cout<<a<<b<<c;
return 0;
}
Compiler error messages:
The name of my file is acro.cpp acro.cpp:
In function âint main()â:
acro.cpp:8:2: warning: âchar* gets(char*)â is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
acro.cpp:8:11: warning: âchar* gets(char*)â is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] acro.cpp:13:29: error: âstrlenâ was not declared in this scope
Since you declared:
char name[100];
then name[i] (if i is a valid index) is a char. But " " is not a character literal, but a string; you should code
if (name[j]==' ')
BTW, you should read more about C++ (and its std::string) and consider declaring
std::string name;
and adapting the rest of your program to make that work. BTW, compile with all warnings & debug info (g++ -Wall -Wextra -g) and learn how to use the debugger (gdb)
Presumably, the compiler told you what's wrong, but you forgot to provide that information. My compiler says
‘strlen’ was not declared in this scope
because you forgot to include the header <cstring> that defines it.
Then it says
ISO C++ forbids comparison between pointer and integer
That's a bit trickier to interpret; it refers to this
if (name[i]==" ")
where you try to compare a character (which converts to an integer) with a string (which converts to a pointer). Instead, compare with another character:
if (name[i]==' ')
^ ^
Once it compiles, get rid of gets(name);. It does the same thing as cin >> name;, but is (perhaps) even more dangerous. Then consider using std::string rather than a C-style char array; that way, your program won't explode if you enter more than 100 characters.
Apart from compiler errors you have done one more mistake :-
gets(name);
char a,b,c;
cin>>name;
second cin would overide first value you have entered using gets.
I am trying to compile a c++ program with multiple files!
mainfile.cc
#include<iostream>
#include "funcfile.hh"
int main()
{
init();
}
funcfile.hh
#include<iostream>
void init();
funcfile.cc
#include<iostream>
#include "funcfile.hh"
using namespace std;
void init()
{
cout<<"hi"<<endl;
}
I am creating the binary as below:
> CC funcfile.cc mainfile.cc -o output
funcfile.cc:
mainfile.cc:
ld: warning: symbol .dynsym[19] has invalid section index; ignored:
(file /usr/local/opt/SunWorkShop/sws_5.0/SUNWspro/lib/libm.so value=19);
And when i execute the output:
> ./output
hi
>
But my concern here is should I care about the message during the compilation:
ld: warning: symbol .dynsym[19] has invalid section index; ignored:
(file /usr/local/opt/SunWorkShop/sws_5.0/SUNWspro/lib/libm.so value=19);
CC version is :
> CC -V
CC: WorkShop Compilers 5.0 98/12/15 C++ 5.0
You are using a compiler that is almost 15 years old. The ELF format has evolved a little bit during that time, it seems that your compiler does not recognize some of the newer special section indexes (see e.g. here, table 7-4). This warning is not something to worry about, but I'd suggest to use a newer compiler, if possible.