Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,946 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,399 people online right now. Registration is fast and FREE... Join Now!




c++ program plzzz help!

 
Reply to this topicStart new topic

c++ program plzzz help!

virgofreak
25 Dec, 2007 - 03:14 AM
Post #1

New D.I.C Head
*

Joined: 19 Dec, 2007
Posts: 5


My Contributions
i have a problem with the second case of this program.....the program is supposed to accept numbers while the user's choice is 'y' n it is supposed to calculate the sum of the digits...plz help!

CODE
# include <iostream.h>
# include <conio.h>
# include <process.h>
void main()
{
    char ch;
    int opt (const int,const int, int &);
    int opt1 (const int,const int,int &);
    clrscr();
    gotoxy (29,02);
    cout<<"M A I N  M E N U "<<endl;
    gotoxy (20,04);
    cout<<"FIBONACCI SERIES.......................1"<<endl;
    gotoxy (20,05);
    cout<<"SUM OF THE DIGITS ENTERED..............2"<<endl;
    gotoxy (20,06);
    cout<<"ASCII CODE OF THE ENTERED CHARACTER....3"<<endl;
    gotoxy (20,07);
    cout<<"EXIT...................................4"<<endl;
    cout<<"\n\n\n\nEnter your choice....";
    ch=getche();
    cout<<"\nThe entered choice is......";
    putch(ch);
    getch();
    switch (ch)
{


        case '2':    clrscr();
                int n=0,sum=0;
                int p= opt1(2,n,sum);
                cout<<"\nThe sum of the entered digits is..."<<p;
                break;
}



int opt1 (const int ch,const int n,int &sum)
{
    char rep='y';
    switch (ch)
    {
        case 2:        while (rep=='y'||rep=='Y')
                {
                    cout<<"\nEnter any number";
                    cin>>n;
                    sum=sum+n;
                    cout<<"\nDo you want to continue ?";
                    cin>>rep;
                }
    }
    return sum;
}


This post has been edited by jjhaag: 6 Jan, 2008 - 10:40 AM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ Program Plzzz Help!
25 Dec, 2007 - 03:27 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Please post your code using the code tags, like this code.gif

A lot of this code is non-standard, so I can't compile it. What are the warnings it gives you? If you can post them, we can point you in the right direction.

You should also note that warnings, although indicative that something might be wrong with the code (e.g. that you may be doing something unintended), are not fatal - they aren't errors, so they won't terminate compilation or prevent you from running the resulting executable.
User is offlineProfile CardPM
+Quote Post

dacoder
RE: C++ Program Plzzz Help!
6 Jan, 2008 - 12:09 AM
Post #3

New D.I.C Head
*

Joined: 6 Nov, 2007
Posts: 5


My Contributions
QUOTE(jjhaag @ 25 Dec, 2007 - 04:27 AM) *

Please post your code using the code tags, like this code.gif

A lot of this code is non-standard, so I can't compile it. What are the warnings it gives you? If you can post them, we can point you in the right direction.

You should also note that warnings, although indicative that something might be wrong with the code (e.g. that you may be doing something unintended), are not fatal - they aren't errors, so they won't terminate compilation or prevent you from running the resulting executable.



Yea... A lot of the code is non-standard. It looks like you don't have any program control taking you back up through your code. When you
CODE
break;
there's nowhere to go.
User is offlineProfile CardPM
+Quote Post

linuxunil
RE: C++ Program Plzzz Help!
7 Jan, 2008 - 01:53 PM
Post #4

New D.I.C Head
Group Icon

Joined: 7 Mar, 2006
Posts: 46



Thanked: 2 times
Dream Kudos: 125
My Contributions
jjhaag is definatly right that alot of your code is non-standard but thats ok if your not using it on other systems. But you should define main as int main() and have it return 0;.

You are also missing a } after the swith statment ending in main.

There should also be a default handle in all of the statments so anything other than 2 will be handled. So you statements would look like
CODE

switch (ch) {


        case '2':    
                clrscr();
                int n=0,sum=0;
                int p= opt1(2,n,sum);
                cout<<"\nThe sum of the entered digits is..."<<p;
                break;
        default:
                break;
    }


This will keep your program from throwing any errors if you give the switch statement anything other than '2'.

Also i really recommend commenting your code alittle more, makes it easier for you and me. It is also good practice to name your variables with meaning names like instead of ch use choice.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 05:07PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month