开发者

C++ Programming Error: expected unqualified-id before "{" token

开发者 https://www.devze.com 2023-03-14 04:21 出处:网络
I am a newbie at C++, andI am trying to make a \"calculator\" which: adds two numbers, sub开发者_JS百科tracts two numbers, multiplies two numbers, divides two numbers, takes the sine of a number, take

I am a newbie at C++, and I am trying to make a "calculator" which: adds two numbers, sub开发者_JS百科tracts two numbers, multiplies two numbers, divides two numbers, takes the sine of a number, takes the cosine of a number, or takes the tangent of a number. Here is the code:

#include <iostream>;
#include <cmath>;
#include <string>
int main () 
{}
int ask(std::string operation);
    {
        std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
        std::cin>>operation;
            if (operation="Addition") 
            {
                goto Add
                                }
    float Add(float addend1, float addend2, float answer) 
    {
    Add:
        std::cout<<"Insert the first number to be added:\n";
        std::cin>>addend1;
        std::cout << "Insert the second number to be added:\n";
        std::cin>>addend2;
        answer=addend1+addend2;
        std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
        break
    }
}

There will be more functions later, but my problem is on line 7. There is an error that says: expected unqualified-id before "{" token. I know my indentation is horrible, but thanks!


You have a lot of issues in your code.

First, as Ivan points out, you are trying to define a function inside of a function (ask() inside main()). That isn't valid.

Second, you have a goto (why?!) attempting to jump to a label in another function. I doubt your compiler will even allow that, but how would you expect that to work? You are attempting to use variables passed to your function addition that don't exist as you never call the function and the stack has never been setup for it. This is bad, don't do it, just call the function properly.

Third, the #include preprocessor directive is terminated with a newline, not a semicolon. That could cause some (relatively) hard to track down compilation errors.

Fourth, you are mistakenly attempting to assign the const char* "Addition" to operation when what you meant to use was the equality operator ==. That won't work ether though because operation is an r-value and cannot be assigned to like that. If you want to modify it you will need to declare it as a pointer, but once again, that's not what you are going for semantically...

If you want to compare strings and (for whatever reason...) are intent on using pointers to char then you should be using strcmp. That said, you are in C++ land, so just use std:string instead.

Try something like this. I haven't enhanced your code in anyway, just made it something that will compile and run. I have made a few changes.

Aside from getting rid of a few syntax errors, your original Add function took the result as a float argument. Assigning to that from within the function would only modify a copy. You would need to take a pointer or reference if you want the caller to see the modified value, but you don't need that at all as you can simply return the result.

The string comparison is case sensitive, so you would probably want to change it to be case insensitive. I'm assuming no localization here :). I'm not performing error checking on the input either, so be aware that it may fail if the user enters something other than a valid floating point number.

#include <iostream>
#include <string>

using namespace std;

void Ask();
float Add( float, float );

int main( size_t argc, char* argv[] )
{
    Ask();
    return 0;
}

void Ask()
{
    cout << "Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";

    string operation;
    cin >> operation;

    if( operation == "Addition" )
    {
        float first = 0, second = 0;
        cout << "enter first operand";
        cin >> first;

        cout << "enter second operand";
        cin >> second;

        cout << "The result is: " << Add( first, second );
    }
}

float Add( float first, float second ) 
{
    return first + second;
}


С++ doesn't allow nested functions. You have function main() and trying to declare function ask() inside it. And compiler doesn't know what you want.


I commented your code a little bit, maybe that gets you started:

#include <iostream>;
#include <cmath>;
#include <string>;
int main () {
    int ask (){         //you cannot nest functions in C++

    char operation [20];    //why not use the string class if you include it anyway 
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
    std:cin>>operation;
    if (operation="Addition"){ //you cannot compare char-strings in C++ like that
    goto Addition;      //don't use goto (I don't want to say "ever", but goto is only used in extremely rare cases) make a function call instead
}
}
    float addition(float addend1, float addend2, float answer)  //you probably want to declare the variables inside the function
{
Addition:           //don't use labels
std::cout<<"Insert the first number to be added:\n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:\n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
}


Let's try to break this down..
You shouldn't use ; on the precompiler directives.

#include <iostream>;
#include <cmath>;
#include <string>;

Should be

#include <iostream>
#include <cmath>
#include <string>

.

int main () {
    int ask (){

See Ivans answer for this

char operation [20];
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std:cin>>operation;
if (operation="Addition"){

You can use std::string instead which is alot easier to deal with. Then you can write

#include <string>

...
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n";
std::string myString;
getline(cin, myString);
if (myString == "Addition"){

.

goto Addition;
}
}
    float addition(float addend1, float addend2, float answer)
{

Not sure what is going on here.. but let's break Addition to it's own function

void Addition(){
    // do addition here
}

.

Addition:
std::cout<<"Insert the first number to be added:\n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:\n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"\n";
}

Don't forget that you have to define the variables

int addend1;
int addend2;
int answer;

Hope this helps you along the way.


First int ask() what is that.Why do you start a block here. Second you have two {s and three }s that's because of the ask(). I think that c++ does not support anonymus functions. Third why do you use goto,when you have a function,just call the function. Fourh your addition func should either be void or remove it's last parameter. Also I think that you don't need string.h file unless you use some rather advanced funcs,the char array should be enough for your program.

0

精彩评论

暂无评论...
验证码 换一张
取 消