else and getline problem

Write your question here.
when I use getline with else it gives error in the loop here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
using namespace std;
class convert {
    public:
        float k_to_m(float km){
            float m;
            m = km*1000;
            return m;
        }


        float m_to_km(float m){
            float km;
            km = m/1000;
            return km;
            }


        void log(){
        string decision;
        cout <<"choose by write (km to m) or (m to km)"<<endl;
        getline(cin,decision );
        /*
        it;s for the decision
        */
        if(decision == "kmtom"){
                // the ends
            float a;
            cout <<"enter the nos "<<endl;
            cin >>a;
            cout <<"the nos is "<<k_to_m(a)<<endl;
            cout<<"-------------------------------------------"<<endl;
            }


        else if(decision == "m to km"){
            float a;
            cout <<"enter the nos  "<<endl;
            cin >>a;
            cout <<"the nos is "<<m_to_km(a)<<endl;
            cout<<"-------------------------------------------"<<endl;}

          else  {
            cout <<"wrong"<<endl<<"-------------------------------------------"<<endl;
        }
        system ("pause");
        }
    };





        int main(){
        for ( ; ;) {convert enter;
         enter .log();}
        }


Last edited on
Did you type it right?
> if (decision == "kmtom") {
> else if (decision == "m to km") {
If you don't get the spacing right, it won't work.

You also need to be wary of mixing getline() and >> operators on the same stream. The stream extractors typically leave the '\n' untouched on the stream, and this will cause getline() to apparently return earlier than expected. For this, we use cin.ignore()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <limits>
using namespace std;
class convert {
public:
  float k_to_m(float km) {
    float m;
     m = km * 1000;
     return m;
  } float m_to_km(float m) {
    float km;
    km = m / 1000;
    return km;
  }

  void log() {
    string decision;
    cout << "choose by write (km to m) or (m to km)" << endl;
    getline(cin, decision);
    /*
       it;s for the decision
     */
    if (decision == "kmtom") {
      // the ends
      float a;
      cout << "enter the nos " << endl;
      cin >> a;
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cout << "the nos is " << k_to_m(a) << endl;
      cout << "-------------------------------------------" << endl;
    }
    else if (decision == "m to km") {
      float a;
      cout << "enter the nos  " << endl;
      cin >> a;
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cout << "the nos is " << m_to_km(a) << endl;
      cout << "-------------------------------------------" << endl;
    }
    else {
      cout << "wrong" << endl << "-------------------------------------------"
          << endl;
    }
  }
};

int main()
{
  for (;;) {
    convert enter;
    enter.log();
  }
}
You got a main function with return type integer, but didn't give a return?!
You got a main function with return type integer, but didn't give a return?

Uniquely, if control reaches the end of the global main function without executing an explicit return statement, the behavior is equivalent to return 0.

This is a "feature" inherited from C. I don't know why it was added in the first place.
Last edited on
it gives error

What error? Syntax or logic?
Hello ahmedddddddd,

Loot at the example you give on line 22. Then compare what is in quotes on line 27 to what is in quotes on line 37.

Then after lines 31 and 40 add this:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.

Hope that helps,

Andy
thank you all of you!!!!!!!!!!!!!!
Topic archived. No new replies allowed.