Having few errors in the code

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1368 Views - Last Post: 13 January 2013 - 04:04 PM Rate Topic: ***-- 2 Votes

#1 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Having few errors in the code

Posted 12 January 2013 - 07:44 PM

 
#include <iostream>
#include <iomanip> 
#include <string> 
#include <stdio.h>

/* kt3 y3_2 */   
/* programm "Aastaaeg" */ 


using namespace std;

int main()
{





int K;  /* Month K  */ 
int A;  /* Season A */
int  jaanuar;
int  veebruar;
int  marts; 
int  aprill;
int  mai; 
int  juuni;
int  juuli;
int  august;
int  september; 
int  oktoober;
int  november;
int  detsember;


cout<<"Sisestage kuu ";               /* Asks for month*/
  cin>> K;                          /* The input K is put in lower cased month name*/
  
  jaanuar = 1;                      /* Giving the value to months to use it to determine season */
  veebruar = 2; 
  marts = 3; 
  aprill = 4; 
  mai = 5; 
  juuni = 6; 
  juuli = 7; 
  august = 8; 
  september = 9; 
  oktoober = 10; 
  november = 11; 
  detsember = 12;
  if ( (K==12) || (K==1) || (K==2));  
  {                  
     cout<<K<<"Aastaaeg on talv.\n"; /*Result when the season is winter*/
  } 
  {
  else if (( K==3 )||(K==4) ||(K==5)); 
  {            // I use else just to show an example 
     cout<<K<<"aastaaeg on kevad.\n";           /*Result when the season is spring*/
  } 
  {
  else if  ((K==6) || (K==7) || (K==8));  
  {            // I use else just to show an example 
     cout<<K<<"aastaaeg on suvi.\n";          /*Result when the season is summer*/
  } 
  {
  else ;
  {
    cout<<K<<"Aastaaeg on sügis.\n";     /*Result when the season is autumn*/
  }
 
}
 
 

getchar(); 
getchar();

} 
}


The errors compiler gives me:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\programs\c\Untitled3.cpp" -o "C:\programs\c\Untitled3.exe" -g3 -I"C:\programs\c\lib\gcc\mingw32\3.4.2\include" -I"C:\programs\c\include\c++\3.4.2\backward" -I"C:\programs\c\include\c++\3.4.2\mingw32" -I"C:\programs\c\include\c++\3.4.2" -I"C:\programs\c\include" -L"C:\programs\c\lib" -g3
C:\programs\c\Untitled3.cpp: In function `int main()':
C:\programs\c\Untitled3.cpp:55: error: expected primary-expression before "else"
C:\programs\c\Untitled3.cpp:55: error: expected `;' before "else"

C:\programs\c\Untitled3.cpp:60: error: expected primary-expression before "else"
C:\programs\c\Untitled3.cpp:60: error: expected `;' before "else"
C:\programs\c\Untitled3.cpp:65: error: expected primary-expression before "else"
C:\programs\c\Untitled3.cpp:65: error: expected `;' before "else"

C:\programs\c\Untitled3.cpp:77: error: expected `}' at end of input
C:\programs\c\Untitled3.cpp:77: error: expected `}' at end of input

Execution terminated
What I have tried?
I tried to find the mistakes and for example try the method of string for hours, but met with no success.

Is This A Good Question/Topic? 0
  • +

Replies To: Having few errors in the code

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Having few errors in the code

Posted 12 January 2013 - 07:50 PM

  if ( (K==12) || (K==1) || (K==2));  
  {                  
     cout<<K<<"Aastaaeg on talv.\n"; /*Result when the season is winter*/
  } 
  {   <<<< DELETE THIS - else should be preceded by if { }
  else if (( K==3 )||(K==4) ||(K==5)); 
  {            // I use else just to show an example 
     cout<<K<<"aastaaeg on kevad.\n";           /*Result when the season is spring*/
  } 
  {    <<<< DELETE THIS, ETC..
  else if  ((K==6) || (K==7) || (K==8));  
  {            // I use else just to show an example 
     cout<<K<<"aastaaeg on suvi.\n";          /*Result when the season is summer*/
  }


When you look through the errors, cpp:55: error: tells you the line number where the error was discovered.
Was This Post Helpful? 2
  • +
  • -

#3 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: Having few errors in the code

Posted 12 January 2013 - 07:51 PM

View Postandrewsw, on 12 January 2013 - 07:50 PM, said:

  if ( (K==12) || (K==1) || (K==2));  
  {                  
     cout<<K<<"Aastaaeg on talv.\n"; /*Result when the season is winter*/
  } 
  {   <<<< DELETE THIS - else should be preceded by if { }
  else if (( K==3 )||(K==4) ||(K==5)); 
  {            // I use else just to show an example 
     cout<<K<<"aastaaeg on kevad.\n";           /*Result when the season is spring*/
  } 
  {    <<<< DELETE THIS, ETC..
  else if  ((K==6) || (K==7) || (K==8));  
  {            // I use else just to show an example 
     cout<<K<<"aastaaeg on suvi.\n";          /*Result when the season is summer*/
  }


When you look through the errors, cpp:55: error: tells you the line number where the error was discovered.

I am quite aware of that but I am incompetent to fix the mistake.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Having few errors in the code

Posted 12 January 2013 - 07:52 PM

If you look through the code that I posted you will see that I have indicated which lines need deleting.
Was This Post Helpful? 1
  • +
  • -

#5 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: Having few errors in the code

Posted 12 January 2013 - 07:56 PM

View Postandrewsw, on 12 January 2013 - 07:52 PM, said:

If you look through the code that I posted you will see that I have indicated which lines need deleting.

Oh, I did not even notice the small additions when I skimmed through your post. My bad :/
I fixed those lines, but this actually increased the total errors from 8 to 10 .

C:\programs\c\Untitled3.cpp: In function `int main()':
C:\programs\c\Untitled3.cpp:55: error: expected primary-expression before "else"
C:\programs\c\Untitled3.cpp:55: error: expected `;' before "else"

C:\programs\c\Untitled3.cpp:60: error: expected primary-expression before "else"
C:\programs\c\Untitled3.cpp:60: error: expected `;' before "else"
C:\programs\c\Untitled3.cpp:65: error: expected primary-expression before "else"
C:\programs\c\Untitled3.cpp:65: error: expected `;' before "else"

C:\programs\c\Untitled3.cpp:77: error: expected `}' at end of input
C:\programs\c\Untitled3.cpp:77: error: expected `}' at end of input

Execution terminated
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Having few errors in the code

Posted 12 January 2013 - 08:01 PM

You've also terminated the ifs prematurely by following them with semi-colons:

if ( (K==12) || (K==1) || (K==2)); <<<< DELETE THIS ; /THESE
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3178
  • View blog
  • Posts: 9,635
  • Joined: 05-May 12

Re: Having few errors in the code

Posted 12 January 2013 - 08:04 PM

If you were more consistent about applying your indentation style, the problem would become self-evident.
Was This Post Helpful? 1
  • +
  • -

#8 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: Having few errors in the code

Posted 12 January 2013 - 08:20 PM

View Postandrewsw, on 12 January 2013 - 08:01 PM, said:

You've also terminated the ifs prematurely by following them with semi-colons:

if ( (K==12) || (K==1) || (K==2)); <<<< DELETE THIS ; /THESE

Thanks for the help, managed to fix it :)

View PostSkydiver, on 12 January 2013 - 08:04 PM, said:

If you were more consistent about applying your indentation style, the problem would become self-evident.

I am not a native speaker, but I think I ran into the problem you described now. What alternatives are there to fix this?

In addition, the program closes right after inserting the month, eventhough I use getchar();
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Having few errors in the code

Posted 12 January 2013 - 08:44 PM

Quote

In addition, the program closes right after inserting the month, eventhough I use getchar();


There is probably a character left in the input buffer which causes the immediate exit. This page discusses some ways to delay exiting of the console. Its trickier to find an acceptable and cross-os solution than you might think!

A sensible way would be to just add a breakpoint at the last line of code.

Cheating, I suppose you could just add:

int dummy;
cin >> dummy;

This post has been edited by andrewsw: 12 January 2013 - 08:46 PM

Was This Post Helpful? 1
  • +
  • -

#10 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: Having few errors in the code

Posted 12 January 2013 - 08:49 PM

I tried your example but the result is the same-the program closes after inserting the month
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Having few errors in the code

Posted 12 January 2013 - 09:04 PM

View Postneedhelpest, on 12 January 2013 - 08:49 PM, said:

I tried your example but the result is the same-the program closes after inserting the month


I posted a link which describes a number of ways to do this. If you are using an IDE you could set a breakpoint, most often by just clicking to the left of a statement. Add return 0; as the last line of code and click to the left of this.. assuming you are using an editor. Actually, 'return 0' on its own might work, and it should be there anyway.

If you are running from the command line (which seems might be the case) then read the information in the link I provided.

This post has been edited by andrewsw: 12 January 2013 - 09:06 PM

Was This Post Helpful? 1
  • +
  • -

#12 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: Having few errors in the code

Posted 12 January 2013 - 09:26 PM

View Postandrewsw, on 12 January 2013 - 09:04 PM, said:

View Postneedhelpest, on 12 January 2013 - 08:49 PM, said:

I tried your example but the result is the same-the program closes after inserting the month


I posted a link which describes a number of ways to do this. If you are using an IDE you could set a breakpoint, most often by just clicking to the left of a statement. Add return 0; as the last line of code and click to the left of this.. assuming you are using an editor. Actually, 'return 0' on its own might work, and it should be there anyway.

If you are running from the command line (which seems might be the case) then read the information in the link I provided.

I did check your post and I had done bit of research beforehand on that matter(http://www.cplusplus.com/forum/articles/6046/).
Yet the program still closes.
On another note, I gave months a numeric value to put them into the if else sentence, but if I want to print the month name out in the last sentence, what should I add to the the code,because right now it gives me a random value of 1994300980 instead of a month name or its number.not to mention the fact that the season is always autumn.I thought that maybe it just multiplies the 12 numerical values, but thats not the case.
If its any help, here is the updated code:
#include <iostream>
#include <iomanip> 
#include <string> 
#include <stdio.h>

/* kt3 y3_2 */   
/* programm "Aastaaeg" */ 


using namespace std;

int main()
{





int K;  /* Month K  */ 
int A;  /* Season A */
int  jaanuar;
int  veebruar;
int  marts; 
int  aprill;
int  mai; 
int  juuni;
int  juuli;
int  august;
int  september; 
int  oktoober;
int  november;
int  detsember;


cout<<"Sisestage kuu: ";               /* Asks for month*/
  cin>> K;                          /* The input K is put in lower cased month name*/
  
  jaanuar = 1;                      /* Giving the value to months to use it to determine season */
  veebruar = 2; 
  marts = 3; 
  aprill = 4; 
  mai = 5; 
  juuni = 6; 
  juuli = 7; 
  august = 8; 
  september = 9; 
  oktoober = 10; 
  november = 11; 
  detsember = 12;
  if ( (K==12) || (K==1) || (K==2))  
  {                  
     cout<<K<<" Aastaaeg on talv.\n"; /*Result when the season is winter*/
  } 
  
  else if (( K==3 )||(K==4) ||(K==5)) 
  {            // I use else just to show an example 
     cout<<K<<" Aastaaeg on kevad.\n";           /*Result when the season is spring*/
  } 
  
  else if  ((K==6) || (K==7) || (K==8))  
  {             
     cout<<K<<" Aastaaeg on suvi.\n";          /*Result when the season is summer*/
  } 
  
  else if  ((K==9) || (K==10) || (K==11))
  {
    cout<<K<<" Aastaaeg on sügis.\n";     /*Result when the season is autumn*/ 
}  else  {
    cout<<K<<" Aastaaeg on sügis.\n";  
 
return 0;
}  
 
 
One more thing, how to edit my own posts,because I cannot find the button to do so. :(/>
}

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Having few errors in the code

Posted 13 January 2013 - 04:19 AM

Your return 0; is in the wrong place - it should be outside the last else clause, so that it will always be executed:

    else if  ((K==9) || (K==10) || (K==11))
    {
        cout<<K<<" Aastaaeg on sügis.\n";     /*Result when the season is autumn*/ 
    }  else  {
        cout<<K<<" Aastaaeg on sügis.\n";  
    }
    return 0;
}


You need to indent your code properly; this will make it much easier to spot such mistakes. Re-instate your getchar() just before 'return 0', or try the other methods I mentioned to prevent the console from closing.

Your code works when I run it. If you want it to write out January, rather than 1, then you could create an array of month names (as strings) and extract the corresponding month-name from this array. You'll need to read up on array and array of strings; it's a bit of a pain!

You have to have made a certain number of posts before you can edit, I can't remember how many.

This post has been edited by andrewsw: 13 January 2013 - 04:22 AM

Was This Post Helpful? 1
  • +
  • -

#14 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: Having few errors in the code

Posted 13 January 2013 - 08:22 AM

View Postandrewsw, on 13 January 2013 - 04:19 AM, said:

Your return 0; is in the wrong place - it should be outside the last else clause, so that it will always be executed:

    else if  ((K==9) || (K==10) || (K==11))
    {
        cout<<K<<" Aastaaeg on sügis.\n";     /*Result when the season is autumn*/ 
    }  else  {
        cout<<K<<" Aastaaeg on sügis.\n";  
    }
    return 0;
}


You need to indent your code properly; this will make it much easier to spot such mistakes. Re-instate your getchar() just before 'return 0', or try the other methods I mentioned to prevent the console from closing.

Your code works when I run it. If you want it to write out January, rather than 1, then you could create an array of month names (as strings) and extract the corresponding month-name from this array. You'll need to read up on array and array of strings; it's a bit of a pain!

You have to have made a certain number of posts before you can edit, I can't remember how many.

Thanks a bunch, I will look up on that.:)
Was This Post Helpful? 0
  • +
  • -

#15 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: Having few errors in the code

Posted 13 January 2013 - 09:32 AM

Hit a brick wall, again.
include <iostream>
#include <iomanip> 
#include <conio.h>

/* kt3 y3_2 */   
/* programm "Aastaaeg" */ 


using namespace std;

int main()
{





int K;  /* Month K  */ 
int A;  /* Season A */
int  jaanuar;
int  veebruar;
int  marts; 
int  aprill;
int  mai; 
int  juuni;
int  juuli;
int  august;
int  september; 
int  oktoober;
int  november;
int  detsember;


cout<<"Sisestage kuu: ";               /* Asks for month*/
  cin>>K;                          /* The input K is put in lower cased month name*/
  

  if  (K==detsember) 
  {                  
     cout<<"Kuu "<< "detsember"<<" aastaaeg on talv.\n"; /*Result when the season is winter*/
  } 
  
  else if  (K==jaanuar)
  {           
     cout<<"Kuu"<<"jaanuar"<<" aastaaeg on talv.\n";           /*Result when the season is spring*/   
  } 
    else if  (K==jaanuar)
  {           
     cout<<"Kuu"<<"veebruar"<<" aastaaeg on talv.\n";           /*Result when the season is spring*/  
  }
       else if ( K==marts ) 
  {            // I use else just to show an example 
     cout<<"Kuu"<<"marts"<<" aastaaeg on kevad.\n";           /*Result when the season is spring*/
  } 
        else if ( K==aprill) 
  {            // I use else just to show an example 
     cout<<"Kuu"<<"aprill"<<" aastaaeg on kevad.\n";           /*Result when the season is spring*/
  }  
        else if ( K==mai) 
  {            // I use else just to show an example 
     cout<<"Kuu"<<"mai"<<" aastaaeg on kevad.\n";           /*Result when the season is spring*/
  } 
  
  else if  (K==juuni)
  {             
     cout<<"Kuu"<<"juuni"<<" aastaaeg on suvi.\n";          /*Result when the season is summer*/
  } 
 else if  (K==juuli)
  {             
     cout<<"Kuu"<<"juuli"<<" aastaaeg on suvi.\n";          /*Result when the season is summer*/
  }  
   else if  (K==august)
  {             
     cout<<"Kuu"<<"august"<<" aastaaeg on suvi.\n";          /*Result when the season is summer*/
  } 
  else if  (K==september)
  {
    cout<<"Kuu"<<"september"<<" aastaaeg on sügis.\n";     /*Result when the season is autumn*/  
  }  
   else if  (K==oktoober)
  {
    cout<<"Kuu"<<"oktoober"<<" aastaaeg on sügis.\n";     /*Result when the season is autumn*/  
  }   
   else if  (K==november)
  {
    cout<<"Kuu"<<"november"<<" aastaaeg on sügis.\n";     /*Result when the season is autumn*/  
        
}  else  {
    cout<<" Sisestasite kuu nime valesti.Palun proovige uuesti.\n";  
}  
getch();
return 0; 
} 

The program always gives out the else clause, whatever the month I write as K.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3