Adding Functions

Adding a function to this program

Page 1 of 1

9 Replies - 1570 Views - Last Post: 27 November 2007 - 11:14 AM Rate Topic: -----

#1 cgraves   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 07

Adding Functions

Post icon  Posted 20 November 2007 - 08:36 AM

Hello All,

I am a begiiner at C++ and this class is killing me! I have to add a function to A and B of the following code. It can be any function because my teacher gave us that option. Any advice, THANKS!
P.S. I need all the help you can give me and I appreciate it!!

#include<iostream>
using namespace std; 
char menuItem;
int number;
int limit, counter;
int temp1, temp2;
const int SENTINEL = -99;
int main() 
{ //open main
while (menuItem != 'C' || 'c')
{ // open while 1
cout << "Please choose from the following options below." << endl;
cout << "A: What's the largest number?" << endl; 
cout << "B: What's the smallest number?" << endl;
cout << "C: Flee to Windows" << endl;
cout << "Please choose now: ";
cin >> menuItem;
cout << endl;
if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
{ // open if main 
switch (menuItem)
{ // open switch 1
case 'a': 
case 'A':
cout << "This section will find the largest number out of your entered numbers? " << endl;
cout << "How many numbers would you like to to enter? ";
cin >> limit;
cout << endl;
cout << "Enter " << limit << " numbers: " << endl;
for (counter = 0; counter < limit; counter++)
{
cin >> number;
if (number > temp1)
temp1 = number;
}
cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl;
cout << endl;
break; 
case 'b':
case 'B':
cout << " Please enter a group of numbers and I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
counter = 0;

temp2 = -99;

 
		  while (number != SENTINEL)

			{			   // open while 3

				counter++;

				cin >> number;

 

				if (number == -99) {

					break;

				}

				if (number < temp2 || temp2 == -99)

					temp2 = number;

			 }

cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
cout << endl;
break;
case 'c':
case 'C':
cout << " Thank you for joining us!! " << endl;
return 0;
} //close switch 1
} // close if main
else
{
cout << "Please enter a valid menu option; A, B, or C." << endl;
cin >> menuItem;
cout << endl;
} // end else 
} // close while 1
return 0;
} // close main

This post has been edited by jjhaag: 20 November 2007 - 01:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding Functions

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6859
  • View blog
  • Posts: 31,604
  • Joined: 10-May 07

Re: Adding Functions

Posted 20 November 2007 - 08:39 AM

When you want to add a function, you must declare it & it's return type outside of main.

#include <stdio.h>

void junk(void);  // Declare the function

int main(void) {
  printf("Welcome to main... this is just a test\n");
  junk();

  return 0;
}

void junk(void) {
  printf("Welcome to junk... this is also just a test\n");
}


Was This Post Helpful? 0
  • +
  • -

#3 cgraves   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 07

Re: Adding Functions

Posted 20 November 2007 - 08:54 AM

Hey, THANKS!

Can you give ma an example of how to do that with the code that I have for A. I think that is my problem. I need to see examples with what I have to see where it goes. I am so lost. THANKS!



View Postno2pencil, on 20 Nov, 2007 - 08:39 AM, said:

When you want to add a function, you must declare it & it's return type outside of main.

#include <stdio.h>

void junk(void);  // Declare the function

int main(void) {
  printf("Welcome to main... this is just a test\n");
  junk();

  return 0;
}

void junk(void) {
  printf("Welcome to junk... this is also just a test\n");
}


This post has been edited by cgraves: 20 November 2007 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 cgraves   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 07

Re: Adding Functions

Posted 20 November 2007 - 09:07 AM

Is that what I type in?

View Postcgraves, on 20 Nov, 2007 - 08:54 AM, said:

Hey, THANKS!

Can you give ma an example of how to do that with the code that I have for A. I think that is my problem. I need to see examples with what I have to see where it goes. I am so lost. THANKS!



View Postno2pencil, on 20 Nov, 2007 - 08:39 AM, said:

When you want to add a function, you must declare it & it's return type outside of main.

#include <stdio.h>

void junk(void);  // Declare the function

int main(void) {
  printf("Welcome to main... this is just a test\n");
  junk();

  return 0;
}

void junk(void) {
  printf("Welcome to junk... this is also just a test\n");
}




Hey everyone, sorry for the multiple posts. I jsut joined the site so I am trying to figure it out. Is the code you gave me what I type in or could you give an example of what to do with the code that I have with any function?
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6859
  • View blog
  • Posts: 31,604
  • Joined: 10-May 07

Re: Adding Functions

Posted 20 November 2007 - 09:20 AM

It is a simple example on how to use multiple functions.
Was This Post Helpful? 0
  • +
  • -

#6 cgraves   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 07

Re: Adding Functions

Posted 20 November 2007 - 09:22 AM

View Postno2pencil, on 20 Nov, 2007 - 09:20 AM, said:

It is a simple example on how to use multiple functions.



Where does it go in the code? Also, do I just plug in what I have for the code? Thank you for the help.
Was This Post Helpful? 0
  • +
  • -

#7 curiose   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: Adding Functions

Posted 20 November 2007 - 10:59 AM

Hi cqraves....i would like to help just if you write for me the question as it is to understand the exact required objects.... and it will be good for you to learn from your mistakes.i will give you a brief explanation about functions as general.
first functions mean:
is as a particular activity usually used to accomplish a specific set of objectives.
Function should follow three rules at atime:
1-Declared (PROTOTYPED)
2-Defined
3-Invoked (Function Calling)
and it has 3 Characteristics:
Function name
Parameters
Return Type
this an axample:
#include <stdio.h>
int main()
{
void message();//declaration
printf ("main function!");
message();//calling
return 0;
}

	void message()//definition
	{
	printf("\n message function");
	}


i think its a good simple example to understand how to use a function in side another function...and there's another techniuqe to use the same values in two functions and more....
i will be happy to hear from you soon ,to help you in solving your program ,but dont forget to write your question:)
Was This Post Helpful? 0
  • +
  • -

#8 cgraves   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 07

Re: Adding Functions

Posted 20 November 2007 - 11:59 AM

Hello curiose and thanks for the help.

I had an assignment to write the following program last week:
#include<iostream>
using namespace std; 
char menuItem;
int number;
int limit, counter;
int temp1, temp2;
const int SENTINEL = -99;
int main() 
{ //open main
while (menuItem != 'C' || 'c')
{ // open while 1
cout << "Please choose from the following options below." << endl;
cout << "A: What's the largest number?" << endl; 
cout << "B: What's the smallest number?" << endl;
cout << "C: Flee to Windows" << endl;
cout << "Please choose now: ";
cin >> menuItem;
cout << endl;
if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
{ // open if main 
switch (menuItem)
{ // open switch 1
case 'a': 
case 'A':
cout << "This section will find the largest number out of your entered numbers? " << endl;
cout << "How many numbers would you like to to enter? ";
cin >> limit;
cout << endl;
cout << "Enter " << limit << " numbers: " << endl;
for (counter = 0; counter < limit; counter++)
{
cin >> number;
if (number > temp1)
temp1 = number;
}
cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl;
cout << endl;
break; 
case 'b':
case 'B':
cout << " Please enter a group of numbers and I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
counter = 0;

temp2 = -99;


while (number != SENTINEL)

{ // open while 3

counter++;

cin >> number;



if (number == -99) {

break;

}

if (number < temp2 || temp2 == -99)

temp2 = number;

}

cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
cout << endl;
break;
case 'c':
case 'C':
cout << " Thank you for joining us!! " << endl;
return 0;
} //close switch 1
} // close if main
else
{
cout << "Please enter a valid menu option; A, B, or C." << endl;
cin >> menuItem;
cout << endl;
} // end else 
} // close while 1
return 0;
} // close main



This week we have to add a function to A and B. It can be any function we just choose. I am confused on even where to start. I just want a simple beginner function to put in. I do not know where to input it in the code either. I learn from actually seeing examples so the book is not helping me. If you caould let e know all of that it would be wonderful, THANKS!

View Postcuriose, on 20 Nov, 2007 - 10:59 AM, said:

Hi cqraves....i would like to help just if you write for me the question as it is to understand the exact required objects.... and it will be good for you to learn from your mistakes.i will give you a brief explanation about functions as general.
first functions mean:
is as a particular activity usually used to accomplish a specific set of objectives.
Function should follow three rules at atime:
1-Declared (PROTOTYPED)
2-Defined
3-Invoked (Function Calling)
and it has 3 Characteristics:
Function name
Parameters
Return Type
this an axample:
#include <stdio.h>
int main()
{
void message();//declaration
printf ("main function!");
message();//calling
return 0;
}

	void message()//definition
	{
	printf("\n message function");
	}


i think its a good simple example to understand how to use a function in side another function...and there's another techniuqe to use the same values in two functions and more....
i will be happy to hear from you soon ,to help you in solving your program ,but dont forget to write your question:)

This post has been edited by jjhaag: 21 November 2007 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#9 curiose   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: Adding Functions

Posted 21 November 2007 - 12:21 PM

Dear cqraves....
try to use the function that you want before the while and link them to the main
#include<iostream>
using namespace std; 
char menuItem;
int number;
int limit, counter;
int temp1, temp2;
const int SENTINEL = -99;
int main() 
{ //open main

			 //declare afunction with name  int funA();
						//call it after that by saying funA() with the same name
	//now open the function int funA() {
//you can include the while and what ever u want inside

while (menuItem != 'C' || 'c')
{ // open while 1
cout << "Please choose from the following options below." << endl;
cout << "A: What's the largest number?" << endl; 
cout << "B: What's the smallest number?" << endl;
cout << "C: Flee to Windows" << endl;
cout << "Please choose now: ";
cin >> menuItem;
cout << endl;
if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
{ // open if main 
switch (menuItem)
{ // open switch 1
case 'a': 
case 'A':
cout << "This section will find the largest number out of your entered numbers? " << endl;
cout << "How many numbers would you like to to enter? ";
cin >> limit;
cout << endl;
cout << "Enter " << limit << " numbers: " << endl;
for (counter = 0; counter < limit; counter++)
{
cin >> number;
if (number > temp1)
temp1 = number;
}
cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl;
cout << endl;
break; 
case 'b':
case 'B':
cout << " Please enter a group of numbers and I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
counter = 0;
temp2 = -99;

// here u can close the funA() but remember the rule we have 
 //to declare the new function int funB() and remember to call it in this 
// position to add the loop after that in it 
//again we have to define the int funB(), the same way of the first

while (number != SENTINEL)
{ // open while 3
counter++;
cin >> number;
if (number == -99) {
break;
}
if (number < temp2 || temp2 == -99)
temp2 = number;
}
cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
cout << endl;
break;
case 'c':
case 'C':
cout << " Thank you for joining us!! " << endl;
return 0;
} //close switch 1
} // close if main
else
{
cout << "Please enter a valid menu option; A, B, or C." << endl;
cin >> menuItem;
cout << endl;
} // end else 
} // close while 1
return 0;
} // close main


I want to add to your information ....that in every calling we are using the same values of the first function in the second and so on
so it must be included in the declaration and calling.... this another example
#include <stdio.h>
int main()
{
int add (int x, int y);//this x&y only for add function and here we declare
int x, y, result;
printf ("Please enter two numbers::");
scanf ("%d %d", &x, &y);
result=add (x, y);//the calling 

printf("\n The result is %d", result);
return 0;
}

int add (int x, int y)//definition
{
int res;
res=x + y;
return (res);//return the result


try them and tell me what happen with you
wish you all the best....sorry for not answering you rapidly...hope that it works
Was This Post Helpful? 0
  • +
  • -

#10 cgraves   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 07

Re: Adding Functions

Posted 27 November 2007 - 11:14 AM

THANKS!!! It worked great

View Postcuriose, on 21 Nov, 2007 - 12:21 PM, said:

Dear cqraves....
try to use the function that you want before the while and link them to the main
#include<iostream>
using namespace std; 
char menuItem;
int number;
int limit, counter;
int temp1, temp2;
const int SENTINEL = -99;
int main() 
{ //open main

			 //declare afunction with name  int funA();
						//call it after that by saying funA() with the same name
	//now open the function int funA() {
//you can include the while and what ever u want inside

while (menuItem != 'C' || 'c')
{ // open while 1
cout << "Please choose from the following options below." << endl;
cout << "A: What's the largest number?" << endl; 
cout << "B: What's the smallest number?" << endl;
cout << "C: Flee to Windows" << endl;
cout << "Please choose now: ";
cin >> menuItem;
cout << endl;
if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
{ // open if main 
switch (menuItem)
{ // open switch 1
case 'a': 
case 'A':
cout << "This section will find the largest number out of your entered numbers? " << endl;
cout << "How many numbers would you like to to enter? ";
cin >> limit;
cout << endl;
cout << "Enter " << limit << " numbers: " << endl;
for (counter = 0; counter < limit; counter++)
{
cin >> number;
if (number > temp1)
temp1 = number;
}
cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl;
cout << endl;
break; 
case 'b':
case 'B':
cout << " Please enter a group of numbers and I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
counter = 0;
temp2 = -99;

// here u can close the funA() but remember the rule we have 
 //to declare the new function int funB() and remember to call it in this 
// position to add the loop after that in it 
//again we have to define the int funB(), the same way of the first

while (number != SENTINEL)
{ // open while 3
counter++;
cin >> number;
if (number == -99) {
break;
}
if (number < temp2 || temp2 == -99)
temp2 = number;
}
cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
cout << endl;
break;
case 'c':
case 'C':
cout << " Thank you for joining us!! " << endl;
return 0;
} //close switch 1
} // close if main
else
{
cout << "Please enter a valid menu option; A, B, or C." << endl;
cin >> menuItem;
cout << endl;
} // end else 
} // close while 1
return 0;
} // close main


I want to add to your information ....that in every calling we are using the same values of the first function in the second and so on
so it must be included in the declaration and calling.... this another example
#include <stdio.h>
int main()
{
int add (int x, int y);//this x&y only for add function and here we declare
int x, y, result;
printf ("Please enter two numbers::");
scanf ("%d %d", &x, &y);
result=add (x, y);//the calling 

printf("\n The result is %d", result);
return 0;
}

int add (int x, int y)//definition
{
int res;
res=x + y;
return (res);//return the result


try them and tell me what happen with you
wish you all the best....sorry for not answering you rapidly...hope that it works

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1