I am really new to C++. I am trying to compile this simple program but I get an error
#include <iostream>
int main(int argc, char** argv)
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}
The error that I get is
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
I try to compile it as g++ file1.cpp
I also tried
g++ -c file1.cpp and then g++ main.exe file1.o
which does not work as well.
What am I doing wrong?
Maybe try and create a new file in an empty directory name it if you want something like main.cpp and put the code in that you needed for namespace and everything else and run
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}
It should work after and output a result
Maybe check out this medium article for more about why you need using namespace std
https://medium.com/breaktheloop/why-using-namespace-std-is-used-after-including-iostream-dc5ae45db652
You can try specify namespace like this ->
#include <iostream>
using namespace std;
EDIT 'must' replaced by 'can try'. It works but why !
Related
In cpp, I need to run a program like this
g++ *.cpp -o out
./out <input.txt> <somenumber>
where input.txt is a text file containing lines of information I need to proccess, and somenumber is an integer value I need to use.
I am searching for hours and couldn't find the answer I was looking for,
I found solutions that work like
./out < input.txt
reads the input.txt line as a string which then in the code I can process,
but the assignment says that the code will be run only and specifically as
./out <input.txt> <somenumber>
can anyone help ?
I have wrote some code, in which I wrote my main as
int main(int argc, char* argv[] ){
but when I run
./out <input.txt>
the terminal gives an error saying
" -bash: syntax error near unexpected token `newline' "
edit: typo
You are correct in wanting to accomplish this using argc and argv. Something like this should work
#include <iostream>
#include <string>
int main(int argc, char* argv[]) {
std::string file_name;
std::string number;
if(argc == 3) {
file_name = argv[1];
number = argv[2];
}
std::cout << "Filename: " << file_name << " number: " << number << "\n";
}
By convention, the use of < > to enclose an input parameter signifies that the parameter is mandatory for the command.
Therefore, the command
./out <input.txt> <some_number>
signifies that the two parameters - input.txt and some_number are mandatory.
The command can be run as:
./out input.txt 101
Here is an example of working code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string inputFile;
string someNumber;
if(argc != 3) {
cout << "Sorry! Wrong input \n";
cout << "Usage: ./out <input_file_name> <some_number> \n";
return -1;
}
inputFile = argv[1];
someNumber = argv[2];
cout << "Processing ...\n";
cout << "File = " << inputFile << ", Number = " << someNumber << "\n";
return 0;
}
Output:
$ ./out input.txt 101
Processing ...
File = input.txt, Number = 101
I get an Debug Assertion Error with expression: nptr!=NULL
my code:
#include <iostream>
using namespace std;
void main(int argc, char* argv[])
{
cout << "Hello Number " << atoi(argv[1]) << endl;
}
can somebody please help me solve this?
Most likely explanation is that you're not passing any parameters to your program, such as you would with the command runme 7.
The argv[argc] string is required to be NULL so this would explain why the assertion is happening.
Check that you have the correct number of parameters before trying to use them:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: runme <integer argument>" << endl;
return 1;
}
cout << "Hello Number " << atoi(argv[1]) << endl;
}
I am trying to print an emoji on standard output console in C++ on Mac OS X environment – clang.
This first code works correctly:
#include <iostream>
#include <cwchar>
int main(int argc, const char *argv[]){
char myEmoji[4] = "⛩";
std::cout << "emoji example: " << myEmoji << std::endl;
return 0;
}
and on console I can see:
./emoji ; exit
emoji example: ⛩
logout
When I try this, it works unexpectedly for me:
#include <iostream>
#include <cwchar>
int main(int argc, const char *argv[]){
wchar_t myEmoji = L'⛩';
std::wcout << "emoji example: " << myEmoji << std::endl;
return 0;
}
and this time I get:
./emoji ; exit
emoji example: logout
Where I am wrong?
I'm trying to do the most basic of things and am hitting a brick wall. I'm trying to read in a file name from the command line to use later in my program, but I can't even seem to extract the name from argv[]. Here's the code:
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char **argv[]){
cout << "argc = " << argc << "\n\n";
cout << "Filename: " << argv[1] << "\n";
return 0;
}
I'm testing it on inputs that supply an argument of course, since there's no error checking. Here's what I get when I run the program:
./a.out testfilename
*
argc = 2
Filename: 0x7fff56e41d30
Now I understand argv[1] is a pointer to an array of chars, so this output makes sense. However, if I make the following change,
cout << "Filename: " << argv[1] << "\n";
to
cout << "Filename: " << *argv[1] << "\n";
in an attempt to dereference argv[1] to pull out the actual string, I get a segmentation fault..
int main(int argc, char *argv[]){
or
int main(int argc, char **argv){
but not
int main(int argc, char **argv[]){
That should be:
int main(int argc, char *argv[])
^ only one * here
When using this simple snippet
int main(int argc, char** argv) {
cout << "Joris" << endl;
return 0;
}
I get randomly this result:
Joris
or
RUN FAILED.
Behaviour occurs with internal terminal or standard output in project settings.
I run under openSUSE Netbeans 7.0.1 with GCC.
Do you get the same result with the following?
#include <iostream>
int main()
{
std::cout << "Joris" << std::endl;
return 0;
}