ok i just startet coding c++ and ofc im geting alot of errors. but until now ive been able to fix them myself but when i try to debug my program it says misplaced else and i just cant find out how to get it to work. so well heres my code:

#include <iostream.h>
#include <conio.h>
main()
{
int kwh,rate1,rate2,base1,base2,rate3,regning;
rate1=22;
rate2=33;
rate3=15;
base1=25;
base2=35;
cout << "indtast hvor mange kwh du bruger: ";
cin >> kwh;
if (kwh<=20)
{
regning=+rate1*kwh;
cout << "din regning er på " << regning << " kr." << endl;
}
else
{
if ((kwh>=20) && (kwh<=40))
regning=+base1+rate2*kwh;
cout << "din regning er på " << regning << " kr." << endl;
}
else
{
if (kwh>=40)
regning=+base2+rate3*kwh;
cout << "din regning er på " << regning << " kr." << endl;
cout << "og du skal nok til at spare lidt på strømmen" << endl;
}
getch();
}

hope some of you can help me with this :).
oh and ofc just corect me in any error you find :P

Recommended Answers

All 8 Replies

If you indent your code correctly you will find the place the error is quite quickly.

#include <iostream.h>
#include <conio.h>
main()
{
  int kwh,rate1,rate2,base1,base2,rate3,regning;
  rate1=22;
  rate2=33;
  rate3=15;
  base1=25;
  base2=35;
  cout << "indtast hvor mange kwh du bruger: ";
  cin >> kwh;
  if (kwh<=20)
  {
    regning=+rate1*kwh;
    cout << "din regning er på " << regning << " kr." << endl;
  }
  else
  {
    if ((kwh>=20) && (kwh<=40))
      regning=+base1+rate2*kwh;
    cout << "din regning er på " << regning << " kr." << endl;
  }
  else // Here's your error
  {
    if (kwh>=40)
      regning=+base2+rate3*kwh;
    cout << "din regning er på " << regning << " kr." << endl;
    cout << "og du skal nok til at spare lidt på strømmen" << endl;
  }
  getch();
}

Please use code tags when you post some code.

can you help me about c++?

about the prog, that is repeating.

prog that is repeating,,

well i knew the error was just dont know how to fix it :/ but thx you for trying anyway.

Try it

// Firstli Don't use the heade
//#include <iostream.h>
// The correct form is

#include <iostream>

using namespace std;

// And very deprecated using for conio
//#include <conio.h>

// main should return int
int main()
{
 /* int  kwh,rate1,rate2,base1,base2,rate3,regning;
  rate1=22;
  rate2=33;
  rate3=15;
  base1=25;
  base2=35;
*/
// Why don't you use like that?

int kwh, rate1 = 22, rate2 = 33, rate3 = 15;
int base1 = 25, base2 = 35, regning = 0;

  cout << "indtast hvor mange kwh du bruger: ";
  cin >> kwh;

    if  (kwh<=20)
    {
    /* regning=+rate1*kwh;
     * you want to add rate1 *kwh to regmimg?
     * so you need to use += operator
     */
        regning += rate1 * kwh; 
        cout << "din regning er på " << regning << " kr." << endl;
    }
    else
    {
        if (kwh >= 20 && kwh <= 40) {
            regning += base1 + rate2 * kwh;
   
            cout << "din regning er på " << regning << " kr." << endl;
        }
        else 
        {
             if (kwh >= 40) {
                 regning += base2 + rate3 * kwh;
        
                 cout << "din regning er på " 
                        << regning << " kr." << endl;
                 cout << "og du skal nok til at spare lidt på strømmen" 
                        << endl;
             }
         }
    }
}

You obviously have three options about kwh, and using if-else is wrong approach.
Instead use:

if (kwh <= 20){
      //do some coding here
}
else if (kwh > 20 && kwh <= 40){
      //do other code here
      //notice that I wrote here kwh > 20, not kwh >= 20
      //that's because kwh == 20 is included already
}
else{
     //some code here if it's not in first two
     //(so it's obviously kwh > 40)
}

arr wirking now :D thx you for the help

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.