Alright, i hope someone can give me a straight answer.

How do i export integers to another function?
Because when i compile and run this code, i get an error telling me:
"

12 D:\test.cpp `numberr' undeclared (first use this function)

The code i used was kinda like this :

int Plus()
{
    
int all = number + number2;

return all;


}





int main()
{
    
    
    int number;
    int number2;
    int do;
    
    
    
    cout << "type in a number" << endl;
    cin >> number;
    
        cout << "another number" << endl;
    cin >> number2;
    
    
    
    
    cout << "+, - , * or /?" << endl;
    cin >> do;
    
    
    if(do == "+")
    {
            
            Plus();
            }
    system("pause");
    
    
    
}

I just learned how to use functions so don't blame me for newbie errors.

So just answer my main question:
How do i use variables that's in main(); in Plus(); ?

BTW, the code is just a test.

Dani AI

Generated

Quick diagnosis: the compiler error "'undeclared' (first use in this function)" means the name you used inside Plus does not exist in that scope. That can be a simple typo (the error message shows numberr, which looks like an extra "r") or the result of trying to use variables that are local to main without passing them. and were right to point you toward passing values as arguments; also highlighted returning values from functions.

Practical fixes and where to learn the rules: give Plus the data it needs via parameters, or have it return a computed value that main stores and prints. Declare the function (a prototype) before main or put the full definition above main so the compiler knows about it. For basic integers, pass-by-value is fine; for larger objects prefer const references. The language rules for functions and parameter passing are documented here: C++ functions.

Operator input and naming gotchas: do not read a + into an int. Use a char or string type for single-character operators, and compare with the correct literal form. Also avoid naming a variable do — that is a C++ keyword used in do/while loops and will cause problems (see iteration statements and keywords: and ). pointed this out and it explains why if(do == "+") would never behave as intended.

Extra tips: check user input for extraction failure (std::cin.fail()), use clear, descriptive names instead of single letters, and avoid globals unless you understand their lifetime and side effects. When stuck, include the exact compiler error and the few lines around it — that makes it much easier to spot a typo or scope mistake.

Recommended Answers

All 5 Replies

You have to pass variables as an argument.
If you want to pass one argument of type char to a function that returns bool you declare function: bool Foo(char a_char) And you call that function like (let's say you already have char letter inside main): Foo(letter); BUT, if you want to catch (to see) what your function RETURNS, then write this: bool fnc_returned = Foo(letter); I hope you won't have a problem implementing this in your code. Have fun :)

Well you need to pass number1 and number2 to your Plus function.

number and number2 are variables which are local to your main function, so Plus does not have access to them.

Try something like this for example:

int Plus(int number, int number2)
{  
int all = number + number2;
return all;
}

int main()
{  
    int number;
    int number2;
    int do;
      
    
    cout << "type in a number" << endl;
    cin >> number;
    
        cout << "another number" << endl;
    cin >> number2;
    
    
    cout << "+, - , * or /?" << endl;
    cin >> do;
    
    
    if(do == "+")
    {
            
            cout << " the total is" << Plus(number, number2) << endl;
            }
    system("pause");
    
    
    
}

>How do i export integers to another function?
Ideally you would import values through parameters and arguments, and export values through the return:

#include <iostream>

int foo() 
{
  return 10;
}

void bar ( int import )
{
  std::cout<< import <<'\n';
}

int main()
{
  bar ( foo() );
}

You should have a read about functions and parameters.

#include <iostream>

int Plus(int x, int y){
    return x + y;
}

int main(){
    int number;
    int number2;
    char op;
    
    std::cout << "type in a number" << std::endl;
    std::cin >> number;
    
    std::cout << "another number" << std::endl;
    std::cin >> number2;
    
    std::cout << "+, - , * or /?" << std::endl;
    std::cin >> op;
    
    if(op == '+'){
            std::cout << Plus(number, number2) << std::endl;
            }
            
    system("pause");
    return 0; 
}

I should point out you cannot read a '+' sign into an int, hence why i changed it to a char. Also you cannot use 'do' as a variable name as it is a command used in do..while loops.

Chris

edit:: damn 4 posts at once!

Thank you guys :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.