Error: expected unqualified-id before '{' token { [closed] - c++

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 4 years ago.
Improve this question
So I've been working on command line arguments in my Computer Science class and I've been getting this:
error: unqualified-id before '{' token {
{
^
I've been trying to figure out what's wrong with it and I can't wrap my head around there being an an error after my first int statement?? Here's the code. Any guidance would be much appreciated! I'm pretty much a noob when it comes to coding.
#include <iostream>
#include <string>
#include <sstream> // for std::stringstream
#include <cstdlib> // for exit()
using namespace std;
double int_rate;
const double RULE72 = 72;
const double YDAYS = 365;
int main(int argc, char *argv[]);
{
printf("argc = %d", argc);
double amount;
double int_rate;
int days;
int years;
cout << "What is the interest rate? ";
cin >> int_rate;
years = RULE72 / int_rate;
cout << "Your money doubles in " << years << " years.";
days = years * YDAYS;
cout << "Your money doubles in " << days << " days." << endl;
cout << "Amount you would like to see double. ";
cin << amount;
cout << "Money earned by day " << (amount * 2) / days << endl;
return 0;
}

A more definitive answer to this problem would be that adding a semicolon (";"), which is treated as end-of-statement (except for the usage of \, which is for splitting lines) in C++ and many other languages... and function names ended with these "semi-colons" are treated as a function declaration... So don't confuse it with function definition which is what you want to do here...
Function declarations are the pre-defined function skeletons that do not have a body and have to be defined somewhere in the code... Or else the compiler will complain about the function not having a body...
Look here:
int main(int argc, char *argv[]); // <- Semicolon
{
// Some useful and useless code here... (but not going to work anyway, so...)
}
Here, the compiler says something like:
"Hey this function has a semicolon at its end, I know, it is a function declaration..."
And when it reaches the next line, it is like:
"Huh!, where's the name of this function, I know I got a semicolon at the previous line, so it couldn't be that!"
And the compiler finally gives an error about the body not having a declaration... So, you have two options to do...
Either do something like this (Highly recommended by everyone)...
int main(int argc, char *argv[]) // <- Semicolon removed
{
// Some useful and useless code here...
}
Or: (Not recommended, but will not cause any problems)
int main(int argc, char *argv[]); // <- Semicolon! Meaning: Declaration!
int main(int argc, char *argv[]) // Compiler says : Oh its the body (definition) of the declaration I read in the previous line...
{
// Some useful and useless code here...
}
Note: I'm sure the compiler would have pointed out to you which line is causing the error so you should try out some methods yourself because learning yourself gains you more experience than yearning for an answer...
Kind regards,
Ruks.

As #StoryTeller said:
int main(int argc, char *argv[]);
This line is causing the problem. Remove ; at the end of this line.

Related

Initialize values in an array of strings c++ [closed]

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
Hello i'm from Indonesia. and i'm verry beginner on C++ programming. I have some problem when i learn about string on C++ . First i declared array of char and i want to initialize a value separately in different command. After i initialize the value my compiler say "Invalid Argument".
#include <iostream>
using namespace std;
int main() {
char Name[5];
Name = "Luke";
cout<<"Character 0 :"<<Name[0]<<endl;
cout<<"Character 1 :"<<Name[1]<<endl;
cout<<"Character 2 :"<<Name[2]<<endl;
cout<<"Character 3 :"<<Name[3]<<endl;
cout<<"Character 4 :"<<Name[4]<<endl;
return 0;
}
sorry if my english is bad :(
A character array(including a C string) can not have a new value assigned to it after it is declared.
The C++compiler interprets these assignment statements as attempts to change the address stored in the array name, not as attempts to change the contents of the array.
However you can use
char name[] = "Luke";
A char[] can't be assigned with a string with the = operator, except for on its initialization. That's why char Name[5]; Name = "Luke"; is invalid while char Name[5] = "Luke"; is.
Assigning strings to char[] can be done with strcpy() / memcpy()-like functions.
So you have two ways of action (assuming you want to work with char[]):
char Name[5] = "Luke";
char Name[5]; strcpy(Name, "Luke"); /* don't forget to #include <string.h>*/
Just for sake of education (since the other answers are on-point to answer the question), here's how I would have written your code to do nearly the same thing.
The changes demonstrate:
used a more appropriate container (a string instead of a char[])
checked for access overruns
moved "one unit of work" into its own subroutine
Code was compiled as C++17 with /usr/bin/clang++ -Weverything -Wno-c++98-compat --std=c++1z:
#include <cstddef>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
void PrintCharAtPos(string const& s, string::size_type pos);
int main() {
auto Name = string{"Luke"};
PrintCharAtPos(Name, 0);
PrintCharAtPos(Name, 1);
PrintCharAtPos(Name, 2);
PrintCharAtPos(Name, 3);
PrintCharAtPos(Name, 4);
return EXIT_SUCCESS;
}
void PrintCharAtPos(string const& s, string::size_type pos) {
if (pos < s.length())
cout << "Character " << pos << " : " << s[pos] << endl;
else
cout << "Character " << pos << " : (out of bounds)" << endl;
}

C++ Passing a local variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I can not figure out where the pointer is wrong in this code. However, I receive the error that the code does not have a pointer-to function.
#include <iostream>
using namespace std;
char uppercase (char ch) {
if ((ch >= 'a') && (ch <= 'z')) {
return ch - 'a' + 'A' ;
cout << "Your capital letter is " << ch << endl;
} else {
return ch;
cout << "Your original letter is: " << ch << endl;
}
}
int main(int& ch){
cout << "Please enter a lowercase letter between a to z: ";
cin >> ch;
char uppercase;
char outChar;
char inChar;
outChar = uppercase(inChar);
system("pause");
}
int main(char&) is not strictly-conforming. It may be provided by the implementation but I don't know of any platform doing this. On a hosted implementation, use int main() or int main(int argc, char** argv) instead.
Building up on the 1st note, declare ch in the function as a local variable and use char not int:
char ch;
Or remove it completely, as described in the 4th point.
You call uppercase on an uninitialized variable (inChar), resulting in undefined behavior because uppercase reads from it. Remove the ch variable and use cin on inChar instead.
You should probably exchange the return ch; with the cout-statement in the uppercase function. The cout-statement is dead code, meaning it will never be executed because the function returns beforehand.
you have local variable with same name as function name: uppercase
get rid of local variable
also fix main function to following signature.
int main(int argc, char *argv[])
{
....
}
ex:
#include <iostream>
using namespace std;
char uppercase (char ch) {
if ((ch >= 'a') && (ch <= 'z')) {
return ch - 'a' + 'A' ;
}
else {
return ch;
}
}
int main(int argc, char* argv[]){
cout << "Please enter a lowercase letter between a to z: ";
char inChar;
cin >> inChar;
char outChar = uppercase(inChar);
return 0;
}
I can see quite a few problems in your code:
Read Why System is evil. In short words, using system("pause") tends to be problematic. There are many other ways to make your code stop (e.g. another cin).
As one of the above comments stated, the general way of opening the main program is by using int main() or int main(int argc, char** argv).
Let's say that your code works... Then you're asking for an executable input "ch" which after that is changed again by asking the same input again during the program's run-time! I proppose that you use point 2. and what #cad said.
The true answer to your problem! You are using as an input to uppercase(char) a variable that has not been filled "InChar", but the character you are filling by user input is "ch".
Hope this helps.

None of my variables are declared (C++) [closed]

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 7 years ago.
Improve this question
int main(int argc, char *argv[]) {
cout << "Ist yoo acited? \n"
char j
cin >> input;
cout << input;
system("PAUSE");
return 0;}
It says in the error message that 'j' is undeclared.
I'm obviously new, and just trying out stuff out.
You're missing a semicolon at the end of j's declaration, and your cout command. You may also wish to provide j with a default value. Also, you don't seem to have declared the variable input.
You're missing a couple semi-colons( ; ) and you also didn't declare input:
int main(int argc, char *argv[]) {
cout << "Ist yoo acited? \n";
char j, input;
cin >> input;
cout << input;
system("PAUSE");
return 0;
}

C++ using arguments ARGV [closed]

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 8 years ago.
Improve this question
I want to take argv[(2 in this example)], store it into vector and use it later in program. The problem is that no operand matches those operands std::string == int. So does that mean that the app sees '-r' as int? I'm a bit confused here.
int main(int argc, char* argv[])
{
std::vector<std::string> argList;
cout<<"argc: "<<argc<<endl;
for(int i=2; i<=argc; i++)
{
argList.push_back(argv[i]);
}
if(argList.at(2) == '-r') cout<<" Good job ";
}
There are several issues with your program:
You iterate i until i == argc, that will attempt to construct a string from argv[argc], a NULL pointer due to the requirement by C and C++ standards that argv[argc] be 0 (NULL), see this SO question. argv is an array with argc pointers to null-terminated character strings (terminated by ASCII NUL, 0), the array itself is terminated with a NULL pointer (not counted in argc). Now, in C++ you can construct a string from a pointer to a null-terminated character string, but passing a NULL pointer to a string constructor results in undefined behavior, see this SO question, also see the list of std::string constructors here, you are implicitly using constructor (4) in that list (from c-string).
You start pushing onto argList with i==2, which means argList[0] will contain argv[2], you then reference argList.at(2), which would correspond to argv[4], this is not likely what you meant.
String literals use double quotes
I've corrected these and created a working program, click here
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
vector<string> argList;
cout << "argc: " << argc << endl;
for(int i=0; i < argc; ++i)
{
argList.push_back(argv[i]);
}
cout << "Program name is " << argList[0] << endl;
if(argc > 1) {
if(argList.at(1) == "-r") {
cout << " Good job, you supplied -r\n";
} else {
cout << "Unrecognized option " << argList[1]
<< "\nUsage: " << argList[0] << " -r\n";
}
}
return 0;
}
The problem is your use of single quotes in '-r'. You want double quotes here:
if(argList.at(2) == "-r") cout<<" Good job ";
The reason is that in C++, single quotes are used for characters only. There is such a thing as a "multi-byte character constant", which is what '-r' ends up being. This is something completely different from a string constant, which is what you want.

How do I create a console application in C++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I wanna make a application like this:
this is console for example:
write_number 5
Your number is 5
How do that?
Can someone explain ?
If you're hoping to learn programming by asking questions on Stack Overflow, you're going to be at it for a long time. I would recommend getting Programming -- Principles and Practice Using C++ or Accelerated C++.
As to your question:
#include <iostream>
#include <cstdlib>
int main(int argc, char** argv)
{
std::cout << "your number is " << std::atoi(argv[1]) << '\n';
}
Please note this is not the best version of this program (eg., what if the user doesn't pass an argument, or doesn't pass a number as an argument, or passes a number larger than an int, or passes a number that is a float or double instead of an int?), but it does give you an idea.
More advanced topics -- without buying the books -- can be found at Bjarne Stroustrup's technical FAQ (Stroustrup created the original versions of C++).
Here's a basic tutorial on cin and cout ("see-in/out")
http://www.cplusplus.com/doc/tutorial/basic_io/
Use console input and output. These are exposed most simply in C++ by std::cin and std::cout:
http://www.cplusplus.com/doc/tutorial/basic_io/
#include <iostream>
int main(int argc, char* argv[])
{
int value;
std::cout << "write_number ";
std::cin >> value;
std::cout << "Your number is " << value << "\n";
return 0;
}
For how best to use these features, check out this FAQ:
http://www.parashift.com/c++-faq-lite/input-output.html
Edit
If you're trying to get command line arguments to your program, such that your session looks like this:
C:\Users\MyUserName> my_program 5
Your number is 5
Then you use the arguments passed to the main function. This is an array of all the parameters you passed to the program when you ran it:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Your number is " << argv[1] << "\n";
}
The arguments passed in are in string (textual) form, though. If you want to convert them to a number, so that you can do arithmatic or perform comparisons with them, here is a way to do that:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
class BadConversion : public std::runtime_error
{
public:
BadConversion(std::string const& s)
: std::runtime_error(s)
{ }
};
template<typename T>
T ConvertTo(std::string const& s)
{
std::istringstream i(s);
T x;
if (!(i >> x))
throw BadConversion("convertTo(\"" + s + "\")");
return x;
}
int main(int argc, char* argv[])
{
int first_parameter = ConvertTo<int>(argv[1]);
std::cout << "Your number is " << first_parameter << "\n";
return 0;
}
How about we step through this.
First, we want to ask the user for input, how would we go about doing this?
// TODO: Ask user for input.
Once we have that input, how would we go about constructing the new string?
// TODO: Make new string.
Now that we have the new string, how do we display it to the user?
// TODO: Display the string.
This leaves use with the following skeleton for you to fill out:
#include <iostream>
int main()
{
// TODO: Ask user for input.
// TODO: Make new string.
// TODO: Display the string.
return 0;
}
To accomplish this, you could use cout, cin, and string. (Of course you could do the string formatting directly in cout as well)
int num = 0;
printf("write_number ");
cin >> num;
cout << "Your number is " << num;
If you're looking to do something a little more interactive, but still want to work in the console, look into curses.
int number = 5;
cout << "Your number" << number;