c++ program ! function

sires,function

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1842 Views - Last Post: 05 January 2011 - 06:08 PM Rate Topic: -----

#1 Guest_someone*


Reputation:

c++ program ! function

Posted 05 January 2011 - 10:52 AM

hi

i have an important program

so this is what i have done so far with it

#include<iostream.h>
void first();//function prototype
void sec();//function prototype
int factorial();//function prototype
char choice;
char ch;
 main()
{
cout<<"enter your choice please\n"
<<"a- count characters\nchange text numbers to integers\nand converts capital character to small\n"
<<"b- sin(x),series\n"
<<"c- count perfects,count digits and prime number\n"
<<"d- binary and decimal\n"
<<"e- Exit\n";
cin>>choice;
if(choice=='a'||choice=='A')
first();//calls function name
else
if(choice=='b'||choice=='B')
void sec();//calls function name
}
void first()
{
int x,ac=0,ec=0,rc=0,y,n=0;
char z;
cout << "press 1 to count characters\n"
<< "press 2 to change text number to integer\n"
<< "press 3 to change from capital to small\n"
<< "press 4 to return to the main menu\n";
cin>>ch;
//while (ch!=4)
if(ch=='1')
{
cout<<"enter your text\n";
//for(;x!='q';)/>
{
cin>>x;
if(x=='a'||x=='A')
ac=ac+1;

else if(x=='r'||x=='R')
rc=rc+1;

else if(x=='e'||x=='E')
ec=ec+1;

cout <<  "a=" << ac
<< " r=" << rc
<< " e=" << ec << endl;
// break;
}
}
if(ch=='2')
{
cout<<"enter your text number\n";
// for(;y!='q';)/>

// y is not character

cin >> y;

if(y>=48 && y<=57)

n=n*10+(y-48);
cout<<"The int is " << n << "\n";
}
if(ch=='3')
{
cout<<"enter your letters\n";
cin >> z;
//use && not or
if(z>='A' && z <='Z') // use the letters not ascii values - easier to read
z = z - ('A'-'a');
cout <<"the letter is\n"<<z;
}
}
void sec()
{
int i,u,n,j;
float sum=0;
int p,f,z=1;
cout<<"enter your choice\n"
<<"1- sin(x)\n"
<<"2- F=n^2n/(2n+1)\n";
//while (ch!=3)
cin>>ch;
if(ch==1)
{cout<<"enter yor sin x values\n";
cin>>u>>n;
for(i=0;i<=n;i+=2)
{
p=1;
f=1;
z=z*-1;
for(j=1;j<=i;j++)
p*=u;
f*=j;
}
sum=sum+(p/(f*1.0))*z;
cout<<sum;
}
}





why when i press b , the sec function does not show up , and why counting char is not right ?

thank you all i hope to find someone to help......

Is This A Good Question/Topic? 0

Replies To: c++ program ! function

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: c++ program ! function

Posted 05 January 2011 - 11:01 AM

try removing the void from before the call to sec()
if(choice=='b'||choice=='B')
void sec();//calls function name


in first() make x a char as that is what you are intending to read (not an int)
void first()
{
char x;
int ac=0,ec=0,rc=0,y,n=0;


This post has been edited by horace: 05 January 2011 - 11:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 Guest_someone*


Reputation:

Re: c++ program ! function

Posted 05 January 2011 - 11:13 AM

yes i don`t know how i missed that thank you

i made x into char , it helped but now it just count the first char you entered not the whole letters like if i entered arerare

it will count only the first a

and the out put will be

a=1 b=0 e=0
Was This Post Helpful? 0

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: c++ program ! function

Posted 05 January 2011 - 11:15 AM

View Postsomeone, on 05 January 2011 - 05:13 PM, said:

yes i don`t know how i missed that thank you

i made x into char , it helped but now it just count the first char you entered not the whole letters like if i entered arerare

it will count only the first a

and the out put will be

a=1 b=0 e=0

rather than x being a char (you only read one character) should it be string or char array - you can then find the number of characters entered
Was This Post Helpful? 0
  • +
  • -

#5 Guest_someone*


Reputation:

Re: c++ program ! function

Posted 05 January 2011 - 11:24 AM

i used to do this without function like this
     cout<<"enter your text\n";
      for(;x!='q';)/>

      {

        cin>>x;
        if(x=='a')
          ac=ac+1;

        else if(x=='r')
          rc=rc+1;
        else if(x=='e')
         ec=ec+1;
        cout <<  "a=" << ac

             << " r=" << rc

             << " e=" << ec << endl;

        // break;
      }
    }


and it worked , counted every letter..

i will try to do it with array


if i can ask you another q , i want to do this sires in this program

f=n^2n/(2n+1)!

thank you for your help
Was This Post Helpful? 0

#6 Guest_someone*


Reputation:

Re: c++ program ! function

Posted 05 January 2011 - 11:25 AM

oh yes and how to return to main menu? :dontgetit:
Was This Post Helpful? 0

#7 davsank  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 29
  • Joined: 05-January 11

Re: c++ program ! function

Posted 05 January 2011 - 11:37 AM

View Postsomeone, on 05 January 2011 - 10:25 AM, said:

oh yes and how to return to main menu? :dontgetit:


to return to the main menu after the function finished it's run, try encapsulating your menu in a do - while loop and adding an Exit option.
For example if exiting the function is option 'e':
#include <stdio.h>
#include <stdlib.h>
int main (void) {
  char usr_selection;
  do{
    puts ("Please enter your choice:");
    getch(usr_selection);
    .
    .
    .
    }while (user_selection != 'e' || user_selection !='E')
  return 0;
}


(written in C but should work for c++ as well)
I hope it helps, if not by the code itself so by the concept.
Sincerely.
David

This post has been edited by davsank: 05 January 2011 - 11:39 AM

Was This Post Helpful? 1
  • +
  • -

#8 Guest_someone*


Reputation:

Re: c++ program ! function

Posted 05 January 2011 - 11:48 AM

thank you David i thought about that to..

i will try it

but for now can anyone plz tell me why the sec() function isn`t working right


void sec()
{
int i,u,n,j,t;
float sum=0;
float s=0;
int p,f,z=1;
cout<<"enter your choice\n"
<<"1- sin(x)\n"
<<"2- F=n^2n/(2n+1)\n";
//while (ch!=3)
cin>>ch;
if(ch==1)
{
cout<<"enter yor sin x values\n";
cin>>u>>n;
for(i=0;i<=n;i+=2)
{
p=1;
f=1;
z=z*-1;
for(j=1;j<=i;j++)
p*=u;
f*=j;
}
sum=sum+(p/(f*1.0))*z;
cout<<sum;
}
if (ch==2)
{
cout<<"enter your sires values\n";
cin>>t;
for(i=1;i<=t;i++)
{
p=1,f=1;
for(j=1;j<=i;j++)
p*=(2*i);
f*=(i+1)*2;
s=p/(f*0.1);
}
cout<<s;
}
}





when i run the program

this comes in the out put

enter your choice
1- sin(x)
2- F=n^2n/(2n+1)

if i choose 1 or 2

it stop

why? i did everything right : (
Was This Post Helpful? 0

#9 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: c++ program ! function

Posted 05 January 2011 - 11:52 AM

in sec() ch is a char so you need to compare it so
cin>>ch;
if(ch=='1')


1 is an int '1' is a char

This post has been edited by horace: 05 January 2011 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: c++ program ! function

Posted 05 January 2011 - 12:01 PM

in C++ you can read a string and count the characters, e.g.
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string data;
    cin >> data;        // read up to first whitespace
    cout << "length " << data.size();
    getline(cin, data); // read a line of characters
    cout << "length " << data.size();
}


Was This Post Helpful? 0
  • +
  • -

#11 Guest_someone*


Reputation:

Re: c++ program ! function

Posted 05 January 2011 - 12:04 PM

thank you horace

you really helped me out
Was This Post Helpful? 0

#12 Guest_someone*


Reputation:

Re: c++ program ! function

Posted 05 January 2011 - 12:12 PM

now i did the 3ed function but i need some help

i now that i annoyed you all but plz it is very important for me


#include<iostream.h>
void first();//function prototype
void sec();//function prototype
void th();//function prototype
char choice;
char ch;
 main()
{
cout<<"enter your choice please\n"
<<"a- count characters\nchange text numbers to integers\nand converts capital character to small\n"
<<"b- sin(x),series\n"
<<"c- count perfects,count digits and prime number\n"
<<"d- binary and decimal\n"
<<"e- Exit\n";
cin>>choice;
if(choice=='a'||choice=='A')
first();//calls function name
else
if(choice=='b'||choice=='B')
sec();//calls function name
else
if(choice=='C'||choice=='c')
th();//function prototype
}
void first()
{
int ac=0,ec=0,rc=0,y,n=0;
char z,x;
cout << "press 1 to count characters\n"
<< "press 2 to change text number to integer\n"
<< "press 3 to change from capital to small\n"
<< "press 4 to return to the main menu\n";
cin>>ch;
//while (ch!=4)
if(ch=='1')
{
cout<<"enter your text\n";
//for(;x!='q';)/>
cin>>x;
if(x=='a'||x=='A')
ac=ac+1;

else if(x=='r'||x=='R')
rc=rc+1;

else if(x=='e'||x=='E')
ec=ec+1;
cout <<  "a=" << ac
<< " r=" << rc
<< " e=" << ec << endl;
// break;
}
//else
if(ch=='2')
{
cout<<"enter your text number\n";
// for(;y!='q';)/>

// y is not character

cin >> y;

if(y>=48 && y<=57)

n=n*10+(y-48);
cout<<"The int is " << n << "\n";
}
if(ch=='3')
{
cout<<"enter your letters\n";
cin >> z;
//use && not or
if(z>='A' && z <='Z') // use the letters not ascii values - easier to read
z = z - ('A'-'a');
cout <<"the letter is\n"<<z;
}
}
void sec()
{
int i,u,n,j,t;
float sum=0;
float s=0;
int p,f,z=1;
cout<<"enter your choice\n"
<<"1- sin(x)\n"
<<"2- F=n^2n/(2n+1)\n";
//while (ch!=3)
cin>>ch;
if(ch=='1')
{
cout<<"enter yor sin x values\n";
cin>>u>>n;
for(i=0;i<=n;i+=2)
{
p=1;
f=1;
z=z*-1;
for(j=1;j<=i;j++)
p*=u;
f*=j;
}
sum=sum+(p/(f*1.0))*z;
cout<<sum;
}
else
if (ch=='2')
{
cout<<"enter your sires values\n";
cin>>t;
for(i=1;i<=t;i++)
{
p=1,f=1;
for(j=1;j<=i;j++)
p*=(2*i);
f*=(i+1)*2;
s=p/(f*0.1);
}
cout<<s;
}
}
void th();
{
int nu,a,i;
float count=0;
cout<<"enter your choice\n"
<<"1- count perfects\n"
<<"2- count digits\n"
<<"3- sum of primes\n"
<<"4- return to the main menu\n";
cin>>ch;
if(ch=='1')
{
cout<<"enter your numbers\n";
cin>>nu;
for(i=1;i<(nu-1);i++)
if(nu%i==0)
sum+=i;
if(sum==nu)
cout<<"number of perfect numbers is:"<<sum;
}
else
if (ch=='2')
{
cout<<"Enter a interger :\n";
cin>>a;
while(a>0)
{
a=a/10;
count++;
}
cout<<"No. of digits in the no entered : \n"<<count;
}
}






i almost done , i apologize if i annoyed you
Was This Post Helpful? 0

#13 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: c++ program ! function

Posted 05 January 2011 - 12:20 PM

for a start you have a ; after the th() - remove it otherwise the code will not compile
void th();
{
int nu,a,i;
float count=0;
cout<<"enter your choice\n"


also indent your code - it will make it easier to read and follow the logical structure (and spot errors!)

This post has been edited by horace: 05 January 2011 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#14 Guest_someone*


Reputation:

Re: c++ program ! function

Posted 05 January 2011 - 12:30 PM

haaah :bananaman:

what a stupid mistake

it runs now thaaaaaaaanx

i just think that the code for the perfect number is wrong
cin>>nu;
for(i=1;i<(nu-1);i++)
if(nu%i==0)
sum+=i;
if(sum==nu)
cout<<"number of perfect numbers is:"<<sum;



Was This Post Helpful? 0

#15 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: c++ program ! function

Posted 05 January 2011 - 12:54 PM

where do you define sum and initialised it to 0? e..g.
int nu,a,i, sum=0;

if you don't initialise the local variable sum to 0 it will have whatever value happens to be in the memory allocated to it
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2