I'm a beginner in C++. I'm not studying it so I don't have a teacher and I don't have a book, a guide. The Internet is my only guide.

Well here's my program:

#include <iostream>
#include <math.h>

using namespace std;

int main ()
{
    int q;
    float x,y;
    
Baslangic:
    cout << "Para Birimi Seciniz.\n";
    cout << " 1: Euro, 2: Amerikan Dolari. Bir Numara Giriniz.\n";
    cin >> q;
    
Euro:
    if (q = 1)
    cout << "Bugunku Euro Kurunu Giriniz: "; cin >> x; "\n";
    cout << "TL Miktarini Giriniz: "; cin >> y; "\n";
    cout << y; cout << " TL "; cout << y/x; cout << " Euro Eder.\n";
    goto Baslangic;
    
Dolar:
    else
    cout << "Bugunku Dolar Kurunu Giriniz: "; cin >> x; "\n";
    cout << "TL Miktarini Giriniz: "; cin >> y; "\n";
    cout << y; cout << " TL "; cout << y/x; cout << " Dolar Eder.\n";
    goto Baslangic;
}

My purpose is to calculate currencies. For instance " X Dollars equal to Y Euros"
And I want to do it this way: First you input "1" or "2" so the program knows what to do. If "1" is pressed, I want it to do the things under "Euro" title. Or else do the things under "Dolar" title.

The sentences in quotes are not relevant. My problem is that I'm not sure if I used if-else correctly. I did a syntax check and it said "expected ';' before 'else'." But there IS a semicolon before "else" so what's the problem?

And I want to know if this expression is correct and proper for my purpose: " if (q = 1)"

Thanks in advance and pardon my broken English.

Recommended Answers

All 5 Replies

You need your curly braces for example:

#include<iostream>


using namespace std;



int main()
{
    int x = 0;
    int y = 0;
    
    
    if ( x != y )
    {
         cout << "x does not equal y";
    }
    else
    {
        cout << "x does equal y";
    }
    
    

  return 0;
}

Just change the values of x and y to see the example. Without them the if and else will only work on the line immediately following these statements.

You will never.. ever.. ever.. see an assignment operation take place in boolean if/else logic.

The only things you will ever use for comparison are the following: <, >, >=, <=, ==, !, !=, && and ||. These are all you are allowed to use.. ever. No exceptions.

Anything else (like a = assignment operator) will always be wrong. Therefore, line #17 in your code is wrong.

So anytime you use if( ), you know you must abide by a very restrictive set of rules.

The problem that you seem to have with this code is understanding the pathway of the if / else construction and the way to define it. There is a "formal" way to define it, but I think that first time it is easier with an example: So here goes:

#include <iostream>
int main()
{
   int x=10;       // Define x to be 10.
   if (x>2)       // This is obviously True
     {         
        std::cout<<" This statement is called"<<std::endl;
        std::cout<<" This is also called"<<std::endl;
     }
   else
     {
        std::cout<<" ****** I am NOT printed " <<std::endl;
        x=20002;        // this does not get done
        std::cout<<"  ***** I am ALSO NOT printed "<<std::endl;
     }
}

I have given a complete program, you can run and see that only the first part is done, but not the second.

If you see there are two pair of brackets { } . After an if statement, or an else, or many other C++ statements, e.g. for(...), a pair of brackets can be written, and this defines the scope of the branch or statement.

In the case above, if you change x to be say 1. Then recompile and run, you will set that the statements after the else are carried out but not any of the statements in the first { }. You can add extra statements to either section but only one will be carried out.

There are two additional points, firstly, IF you write many C++ statements, e.g. if etc, AND do not follow it by an open {, then it is as if you have written a { } round the next statement. Let me give an example

#include <iostream>
int main()
{
   int x=1;
   if (x>2)
     std::cout<<"X == "<<x<<std::endl;
   std::cout<<"This is always printed"<<std::endl;
  
   // This is a repeat of the code above:
   if (x>2)
     {
       std::cout<<"X == "<<x<<std::endl;
     }
   std::cout<<"This is always printed"<<std::endl;
}

Note that there are two copies of the same idea, BUT written slightly differently, it is a matter of style which to write, however, you will see both.

Finally, there is the fact that you can embed another group of statements in another, again let us have an example:

#include <iostream>
int main()
{
   int x=1;
   int y=20;
   if (x>2)
     {
       std::cout<<"I get printed if x is big"<<std::end;;
       if (y>5)
         {
            std::cout<<"I get printed ONLY if x>2 and y > 5 "<<std::endl;
         }
     }
  std::cout<<"code finished : "<<std::endl;
}

Run that code twice, once with x as given and once with x > 2. You will not that the
inner part is ONLY run when the first condition is met. i.e. x>2.

Hope that helps, run the examples, and then have another go at your program.

I'm a beginner in C++. I'm not studying it so I don't have a teacher and I don't have a book, a guide. The Internet is my only guide.

Well here's my program:

#include <iostream>
#include <math.h>

using namespace std;

int main ()
{
    int q;
    float x,y;
    
Baslangic:
    cout << "Para Birimi Seciniz.\n";
    cout << " 1: Euro, 2: Amerikan Dolari. Bir Numara Giriniz.\n";
    cin >> q;
    
Euro:
    if (q = 1)
    cout << "Bugunku Euro Kurunu Giriniz: "; cin >> x; [B]"\n";[/B]
    cout << "TL Miktarini Giriniz: "; cin >> y; [B]"\n";[/B]
    cout << y; cout << " TL "; cout << y/x; cout << " Euro Eder.\n";
    goto Baslangic;
    
Dolar:
    else
    cout << "Bugunku Dolar Kurunu Giriniz: "; cin >> x; [B]"\n";[/B]
    cout << "TL Miktarini Giriniz: "; cin >> y; [B]"\n";[/B]
    cout << y; cout << " TL "; cout << y/x; cout << " Dolar Eder.\n";
    goto Baslangic;
}

My purpose is to calculate currencies. For instance " X Dollars equal to Y Euros"
And I want to do it this way: First you input "1" or "2" so the program knows what to do. If "1" is pressed, I want it to do the things under "Euro" title. Or else do the things under "Dolar" title.

The sentences in quotes are not relevant. My problem is that I'm not sure if I used if-else correctly. I did a syntax check and it said "expected ';' before 'else'." But there IS a semicolon before "else" so what's the problem?

And I want to know if this expression is correct and proper for my purpose: " if (q = 1)"

Thanks in advance and pardon my broken English.

Well, there are lots of syntactic errors in your code..try to avoid goto and write your code with if - else statements. Expression if (q = 1) is not correct. When you check equality between two integer values then you have to use operator ==. Additionally some escape sequences "\n" don't make any sense in your code..Delete them and generally try to rewrite your code.

It seems that I need to learn; not to try and make it. Thanks everyone.

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.