help on code

C, C++, Visual C++, C++.Net Topics
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

help on code

Post by Face » Mon Nov 15, 2010 6:18 pm

I wrote a code to test some codes I learn,This is it.It doesn't get any compiling errors.But don't perform well.

Code: Select all

/* I am going to make a phone derectory
made by G-sparkZ*/

#include<iostream>
using namespace std;

#define D 077
#define M 071
#define E 072

int main ()
{
    int num;
    char x;
    cout<<"DIALOG-D"<<endl<<"MOBITEL-M"<<endl<<"ETISALAT-E"<<endl;
    cout<<"ENTER THE CODE OF YOUR CONECTION -"<<endl;
    cin>>x;
    if(x==D){
                cin>>num;
                cout<<"DIALOG NUMBER -"<<D<<num<<endl;
                }
                else if(x==M){
                     cin>>num;
                     cout<<"MOBITEL NUMBER -"<<M<<num<<endl;
                     }
                     else(x==E);{
                                   cin>>num;
                                   cout<<"ETISALAT NUMBER -"<<E<<num<<endl;
                                   }
                                   system("pause");
                                   return 1;
                                   
    
    }
I wanna display with the full number.I define D,M,E as numbers.So I wanna get out put like this.
077XXXXXXX
Help me on this.What is the error I have made.
I could make this in this way.I could assign some values for int D,M,E then I could make this.But I tried to test defining
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help on code

Post by Neo » Mon Nov 15, 2010 6:59 pm

You need to use single quotes for character values.

Code: Select all

if (x == 'D') {

}
Defining them as 077, etc... is incorrect. Since you have used #define directive, the compiler replaces all D's with 077 and your program will become as below before compile.

Code: Select all

if(x==D){
will become,

Code: Select all

if(x==077){
Since 077 is a number, it will again become as below,

Code: Select all

if(x==77){
Here the problem is, you are not asking the user to input as 77, 71, etc... You ask the user to input D, M, etc...
In this case, the correct way is to use single quotes.

One thing for your note. Have you gone through the C++ tutorial that Nipuna has mentioned? I see you miss some important points.

It is quite easier for a learner to follow a good tutorial and ask questioned based on that. Step-by-step leaning on concepts is really required when it comes to programming. Otherwise you'll miss important points. To write a good programme, thorough knowledge in almost all concepts in programming is required. After going through the tutorial, you can write your own codes and experiment with your knowledge. Otherwise, it will make you too tired due to lack of knowledge.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help on code

Post by Face » Mon Nov 15, 2010 7:09 pm

Thanks for your help NEO.I got it.I am addicting to write little codes.It makes me happy.So I completely forgot to follow that tutorial.I was writing codes in all the times.I made a code according to char variables.But in that I can't calculate (T)tempreture.It always give me answer 0 to (t).Q,M,C working properly.
can you tell me why?

Code: Select all

//calculate Q=MCT

#include<iostream>
using namespace std;

int main(){
    int m,c,t,q,z;
    char x;
    cout<<"We are goig to calculate Q=MCT"<<endl<<endl;
    cout<<"Enter what you wanna calculate Q or M or C or T -"<<endl<<endl;
    cin>>x;
    if(x=='q'){
             cout<<"enter your dimentions - "<<endl;
             cout<<"M - ";
             cin>>m;
             cout<<endl<<"C - ";
             cin>>c;
             cout<<endl<<"T - ";
             cin>>t;
             q=m*c*t;
             cout<<endl<<"Your anser for Q - "<<q;
             cout<<endl<<"thank you for using this program ";
             system("pause");
             return 0;
             }
             else if(x=='m'){
                  cout<<"enter your dimentions - "<<endl;
                  cout<<"Q - ";
                  cin>>q;
                  cout<<endl<<"C - ";
                  cin>>c;
                  cout<<endl<<"T - ";
                  cin>>t;
                  z=(c*t);
                  m=q/z;
                  cout<<endl<<"Your anser for M - "<<m;
                  cout<<endl<<"thank you for using this program ";
                  system("pause");
                  return 0;
                  }
                  else if(x=='c'){
                       cout<<"enter your dimentions - "<<endl;
                       cout<<"Q - ";
                       cin>>q;
                       cout<<endl<<"M - ";
                       cin>>m;
                       cout<<endl<<"T - ";
                       cin>>t;
                       z=(m*t);
                       c=q/z;
                       cout<<endl<<"Your anser for c - "<<c;
                       cout<<endl<<"thank you for using this program ";
                       system("pause");
                       return 0;
                       }
                       else(x=='t');{
                                  cout<<"enter your dimentions - "<<endl;
                                  cout<<"Q - ";
                                  cin>>q;
                                  cout<<endl<<"M - ";
                                  cin>>m;
                                  cout<<endl<<"C - ";
                                  cin>>t;
                                  z=(m*c);
                                  t=q/z;
                                  cout<<endl<<"Your anser for T - "<<t;
                                  cout<<endl<<"thank you for using this program ";
                                  system("pause");
                                  return 0;
                                  }
                                  system("pause");
                                  return 0;
    
    }
Sorry if I am wasting your time by asking same little questions.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help on code

Post by Neo » Mon Nov 15, 2010 7:22 pm

Your indents are 100% wrong. Can you follow http://en.wikipedia.org/wiki/Indent_style and re-arrange your code with proper indents. It wastes quite a lot of time to read a programme without proper indents.

Ex: for "if"

Code: Select all

if ( condition1 ) {
    if ( subcondition ){
    }
    else{
    }
}
else if ( condition1 ) {

}
else {
}
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help on code

Post by Face » Mon Nov 15, 2010 7:32 pm

oops..my mistake.Sorry bro..
May be this is correct.I made it by giving tabs in DEV

Code: Select all

//calculate Q=MCT

#include<iostream>
using namespace std;

int main(){
    int m,c,t,q,z;
    char x;
    cout<<"We are goig to calculate Q=MCT"<<endl<<endl;
    cout<<"Enter what you wanna calculate Q or M or C or T -"<<endl<<endl;
    cin>>x;
    
    if(x=='q'){
               cout<<"enter your dimentions - "<<endl;
               cout<<"M - ";
               cin>>m;
               cout<<endl<<"C - ";
               cin>>c;
               cout<<endl<<"T - ";
               cin>>t;
               q=m*c*t;
               cout<<endl<<"Your anser for Q - "<<q;
               cout<<endl<<"thank you for using this program ";
               system("pause");
               return 0;
    }
    else if(x=='m'){
         cout<<"enter your dimentions - "<<endl;
         cout<<"Q - ";
         cin>>q;
         cout<<endl<<"C - ";
         cin>>c;
         cout<<endl<<"T - ";
         cin>>t;
         z=(c*t);
         m=q/z;
         cout<<endl<<"Your anser for M - "<<m;
         cout<<endl<<"thank you for using this program ";
         system("pause");
         return 0;
    }
    else if(x=='c'){
          cout<<"enter your dimentions - "<<endl;
          cout<<"Q - ";
          cin>>q;
          cout<<endl<<"M - ";
          cin>>m;
          cout<<endl<<"T - ";
          cin>>t;
          z=(m*t);
          c=q/z;
          cout<<endl<<"Your anser for c - "<<c;
          cout<<endl<<"thank you for using this program ";
          system("pause");
          return 0;
    }
    else(x=='t');{
                   cout<<"enter your dimentions - "<<endl;
                 cout<<"Q - ";
                 cin>>q;
                 cout<<endl<<"M - ";
                 cin>>m;
                 cout<<endl<<"C - ";
                 cin>>t;
                 z=(m*c);
                 t=q/z;
                 cout<<endl<<"Your anser for T - "<<t;
                 cout<<endl<<"thank you for using this program ";
                 system("pause");
                 return 0;
    }
    
    system("pause");
    return 0;
    
    }
Please don't be angry with me... :shock:
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help on code

Post by Neo » Mon Nov 15, 2010 7:48 pm

Not angry...just trying to get you on the right track :) . I'm very strict on programming standards.

Can't you see the mistake now?

Code: Select all

else(x=='t');{
Change it to (remove the invalid semicolon that made the compiler to terminate the else from there and execute next part with parentheses always),

Code: Select all

else if(x=='t'){
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help on code

Post by Face » Mon Nov 15, 2010 8:20 pm

Thanks for helping me.I got a error while compiling without that (;)
MCT.png
MCT.png (38.67 KiB) Viewed 8178 times
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help on code

Post by Neo » Mon Nov 15, 2010 9:13 pm

It is still 'else' in your code. Should be changed to 'else if'. Otherwise you can't give a condition.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help on code

Post by Face » Mon Nov 15, 2010 9:17 pm

Ohh...I didn't see that.Thanks.But still don't perform in correct way.always give 0 as answer.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help on code

Post by Neo » Tue Nov 16, 2010 10:01 am

Submit the corrected code again.
Post Reply

Return to “C/C++ Programming”