0

can someone help me? im just starting programming on C/C++ (im using DevC++)

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[]) {
    int X;
    int Y;
    int R;
    cout << "FUNCION POTENCIA";
    cout << "Ingrese primero la base y luego el exponente";
    cin >> X, Y;
    if (Y = 1) {
        R = X;
    };
    //if
    else() {
        do
            while (Y > 0) {
                R = X * X;
                Y--;
            }; //do while
    }; //else   cout<< R;
    system("PAUSE");
    return EXIT_SUCCESS;
} //main
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Marco
  • 1,112
  • 1
  • 14
  • 34
  • `else()` is wrong, it should be `else`. Please read a few tutorials about the subject before asking questions here. And welcome to Stack Overflow! – holgac Apr 13 '15 at 20:38
  • remove ; before else, also there should not be any parenthesis with else (else() is wrong) – cosmos Apr 13 '15 at 20:38
  • 5
    Unforutunately you have syntax errors all over the place. If you fix that one you will just get the next one. Probably better if you go back and read a beginner's tutorial. Using stackoverflow as a line by line syntax checker isn't going to work well :-) – kaylum Apr 13 '15 at 20:39
  • 2
    C/C++ isn't a language. Judging by your code it seems you're learning C++. – Emil Laine Apr 13 '15 at 20:50

3 Answers3

2

You have five errors:

  1. cin >> X, Y; doesn't read user input to both X and Y. It's actually two separate statements: cin >> X; and Y;. The first one reads user input to X, the second one evaluates the current value of Y and doesn't do anything to it. To read user input to both variables: cin >> X >> Y;.
  2. if (Y = 1) will assign 1 to Y. You want to check for equality with ==: if (Y == 1) i.e. "if Y is equal to 1".
  3. Don't put semicolons after if, else, and while blocks. They're redundant and cause errors like this. Put semicolons only after statements.
  4. Don't put () after else.
  5. A do-while loop is do { ... } while (...); not do while (...) { ... }, whereas a while loop is simply while (...) { ... }.

Here's the correct version:

...
cin >> X >> Y; // 1.

if (Y == 1) { // 2.
    R = X;
} // 3.
else { // 4.
    while (Y > 0) { // 5.
        R = X * X;
        Y--;
    }

    // or if you want a do-while:
    do {
        R = X * X;
        Y--;
    } while (Y > 0); // semicolon here!
}

In conclusion, pick a book to learn the language properly, so you can stop guessing what the right syntax is.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

It's because you have a semicolon after your if statement. Correct if statement form is:

if(expression)
{
     // If Code
}
else
{
     // Else code
}
Bob
  • 1,779
  • 3
  • 22
  • 35
  • Remove the () after the else, and the pretty much any semicolon that immediately follows a close bracket. I could be forgetting something but I don't think }; is ever correct syntax. – Bob Apr 13 '15 at 20:43
0
 if (Y = 1) {
        R = X;
 };

Remove the last ";" here.

victor175
  • 624
  • 3
  • 10
  • 23