Testing out online IDE

I recently got a Chromebook, which I can't download exe's and stuff like that to. So I'm testing out CodeChef. I wrote some simple code that seems like it should work, but it's telling me that my variables are not declared in this scope.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>

int getInput(int x);

int main() 
{
    std::cout << "Type a number: " << getInput(x) << std::endl;
    
    std::cout << "You chose " << x << std::endl;
    
}

int getInput(int x) 
{
    calcInt = 0;
    std::cin >> calcInt;
    x = calcInt;
    
}
I can see what you are intending to do, but there are problems with undeclared variables. Also getting the input doesn't belong in the middle of a cout << statment.



Here are two variations based in your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int getInput();

int main() 
{
    std::cout << "Type a number: ";
    
    int x = getInput();
    
    std::cout << "You chose " << x << std::endl;
    
}

int getInput() 
{
    int calcInt = 0;
    std::cin >> calcInt;
    return calcInt;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void getInput(int & x);

int main() 
{
    std::cout << "Type a number: ";
    
    int x;
    getInput(x);
    
    std::cout << "You chose " << x << std::endl;
    
}

void getInput(int & x) 
{
    std::cin >> x;
}

Topic archived. No new replies allowed.