Programmimg difficulities

Thread Starter

nanobyte

Joined May 26, 2004
120
I am learning C++. I am trying to make a program that prompts the user to enter three decimals and returns the largest of the three. I have a header file and the actual program file. When I try to run the program on Borland C++ Builder 6 it tells me there is an Declaration Synatx error on the 15th line of the program file. Could somebody tell me what the heck is going on! The first attachment is the program file that has the error and the second attachment is the header file that I believe is alright since no errors are shown for it. I'm already behind as it is so can somebody help me! :( :(
 

Attachments

haditya

Joined Jan 19, 2004
220
are all variables used declared in the start of the program---i dun knwo much bout c++ but in c one has to declare all varibles used at the start of the program... thats wat we call type declarations
 

Perion

Joined Oct 12, 2004
43
You've got multiple function declarations and definitions all over the place and have mixed up what the header and program files should do. You have two main() definitions (???), and have multiple declaraions for larGest(), called the function from one of the main() functions, but never defined it anywhere.

Here's a suggestion:

In your header (.h) file just declare your function protoypes (at least for this particular program), then define those functions in the program file and fix the two main() functions so you have one coherent main().

Here's an example header:

Rich (BB code):
//***** A header file - badprogrammyheader.h ******
#ifndef __badprogrammyheader_h //Prevents multiple includes
#def __badprogrammyheader_h
//Declare your function prototypes
void myHeader();
double larGest(double, double, double);
#endif
//********* That's it for your header file ********
And for your cpp program file, maybe something like this:
Rich (BB code):
//
//Include all the headers you need for the program -
//or you could put the standard header includes in your
//badprogrammyheader_h and then just include your header
//in this file.
//
#include <conio.h>
#include <iostream>
#include "badprogrammyheader_h"

//Declare any global variables
//(there are none)


//Define your main() function
 int main()
 {
 double uno;
 double dos;
 double tres;

 clrscr();
 myHeader(); // Call your first function
 //
 // Any other code?
 //
 
 
 //Prompt the user
 cout << "Put three numbers in before I buss your head in" <<endl;
 cin >> uno >> dos >> tres;

 cout << "The largest of the the three numbers is "
    << larGest(uno, dos, tres) << endl;

 return 0;
}
 
//myHeader definition 
 void myHeader()
{
 cout << "e-money" <<endl
   << "October 14, 2004" << endl;
}

//Now - you need to define the code for this function
double larGest(uno, dos, tres)
{
// Put your code here.
}
Perion
 

Thread Starter

nanobyte

Joined May 26, 2004
120
Thanks for the info. I have the program running nicely now. I am now trying to add a little more code to the program. After showing the largest of three numbers I want it to take that number and set it to a power that will be the smallest number. Then after that I want the program to take that result and set it to the power of the second largest number. Follow me? Could y'all help me with figuring out how to go about doing this. :) An example like what Perion gave me before would be greatly appreciated. ;)
 

Thread Starter

nanobyte

Joined May 26, 2004
120
Hey, I'm waiting on somebody to help me out here. The attachment below is a copy of the program that I want to add to. The first bit of code is the header file and the code that is about five lines below it is the actual program file. Could somebody help me out, I'm still new to programming. :unsure:
 

Attachments

alzaro

Joined Oct 26, 2004
1
Originally posted by nanobyte@Oct 14 2004, 02:34 PM
I am learning C++. I am trying to make a program that prompts the user to enter three decimals and returns the largest of the three. I have a header file and the actual program file. When I try to run the program on Borland C++ Builder 6 it tells me there is an Declaration Synatx error on the 15th line of the program file. Could somebody tell me what the heck is going on! The first attachment is the program file that has the error and the second attachment is the header file that I believe is alright since no errors are shown for it. I'm already behind as it is so can somebody help me! :( :(
[post=2939]Quoted post[/post]​
hi I went through your code briefly man, I think the problem is at where you declared your double function"double larGest(double, double, double);"
UH... I belive you have to name the your double variables in your parameters so lets say you decided to name them n1 through n3 so your function definition should become "double largGest(double n1, double n2, double n3);", and then your function call should be "double largGest(n1, n2, n3)"

see the problem is that you were using the variable type "double" as your paramters name and that doesn't work in C++

I hope I am right
 

Thread Starter

nanobyte

Joined May 26, 2004
120
Thanks for the response alzaro, but that wasn't the info I was looking for. I don't have any problems with my program anymore alzaro. The last attachment I gave is a the program that is working. My problem is that I am now trying to add a little more code to the program. After showing the largest of three numbers I want it to take that number and set it to a power that will be the smallest number. Then after that I want the program to take that result and set it to the power of the second largest number. :( Please somebody help me with this; it's driving me crazy! Check out the attachment below!
 

Attachments

Perion

Joined Oct 12, 2004
43
Originally posted by nanobyte@Oct 29 2004, 04:55 PM
Thanks for the response alzaro, but that wasn't the info I was looking for.  I don't have any problems with my program anymore alzaro.  The last attachment I gave is a the program that is working.  My problem is that I am now trying to add a little more code to the program. After showing the largest of three numbers I want it to take that number and set it to a power that will be the smallest number. Then after that I want the program to take that result and set it to the power of the second largest number.  :(  Please somebody help me with this; it's driving me crazy!  Check out the attachment below!
[post=3186]Quoted post[/post]​
There's a zillion ways to do this. Here's one.

Instead of a function - double larGest(double,double,double) - change it to a function that takes the three doubles, sorts them into descending order, and assigns them, largest to smallest, to successive elements of a global array of doubles - maybe the array is called g_dArray. Then you can just use the first element g_dArray[0], which contains the largest double, like you did before. Then use the pow function to raise g_dArray[0] to the power of g_dArray[2] - i.e. the largest to the power of the smallest. I assume you have the pow function. It's like the following and declared in math.h so you need to include the math.h header file:

double pow(double x, double y); //Raises x to the power of y and returns the result.

So GLOBALLY declare an array of three doubles.

Rich (BB code):
// A global array of three doubles
double g_dArray[3];
Then change your larGest function to a function that sorts three doubles in descending order and places them in the global array's sequential elements.

Rich (BB code):
void OrderArray(double n1, double n2, double n3)
{
  // Write code to sort n1, n2, and n3, assigning the largest to g_dArray[0], 
  // the next largest to g_dArray[1], and the smallest to g_dArray[2]. Of
  // course, equal numbers are easy to deal with.
  // 
  // I ain't gonna write the code - you'll enjoy coming up with some
  // good, efficient, sorting code. Use the array for temporary storage. 
  // When complete, g_dArray[0] >= g_dArray[1] >= g_dArray[2]
  }
Now you can cout the largest of the three like before using g_dArray[0].

Next, just send the pow function whatever numbers you want raised to what powers and store the results in some double variables that you can use for whatever you had in mind:

Rich (BB code):
// After calling OrderArray(n1,n2,n3) declare some variables for
// storage of the results of calls to the pow function
double dResult1, dResult2; 

// Here's the largest number raised to the power of the smallest number 
dResult1 = pow(g_dArray[0], g_dArray[2]);

// Now take that number and raise it to the power of the middle number.
dResult2 = pow(dResult1, g_dArray[1]);

// Do whatever with the dResult1 and dResult2 variables
(Check out the pow function in your c++ reference. Watch out for overflow and underflow conditions and also make sure you send pow values that it can handle.)

Perion
 

Thread Starter

nanobyte

Joined May 26, 2004
120
Well I have diligently worked on this program for some time and now have two parts of it kinda working. As you may recall I am trying to make the program:
1. Prompt the user to enter 3 numbers and show the largest of the three.
2. Take the largest number and raise to the smallest number.
3. Take that result and raise it to the middle number.

I have step1 working, but step2 isn't doing what it's supposed to do. Instead of taking the largest number and raising it to the smallest number, it is taking the last number entered and raising it to the first number entered no matter if it's the largest, smallest, or the middle number or what order the numbers are in.

The attachment below is the program. The first bit of code is the program file and the code about 8 to 10 lines below it is the header file. Can someone please take a look and tell me what the heck is going. :huh:
 

Thread Starter

nanobyte

Joined May 26, 2004
120
:( Can someone please help me with the program I am trying to create in the above post. Perion gave me some suggestions with arrays, but I am trying to stick with using functions.
 

Perion

Joined Oct 12, 2004
43
Originally posted by nanobyte@Nov 24 2004, 10:02 AM
:( Can someone please help me with the program I am trying to create in the above post. Perion gave me some suggestions with arrays, but I am trying to stick with using functions.
[post=3763]Quoted post[/post]​
Using a global array of doubles to hold the results of sorting was just one possibility. I'd definitly define a sort function (instead of your larGest function) that takes uno, dos, and tres as parameters and sorts them into decending order. If you use pointers to doubles or better yet, reference parameters, in the sort function parameter list it will then modify the variables passed to it.

Here's an example using reference parameters:

Rich (BB code):
double uno;
double dos;
double tres;

cout << "Put three numbers in before I buss your head in." <<endl;
cin >> uno >> dos >> tres;

void sort(double& d_one, double& d_two, double& d_three);  //function prototype

sort(uno, dos, tres); // Call the sort function. When it returns, uno 
               // will contain the largest, dos the middle, 
               // and tres the smallest numbers.

cout << "\nThe largest of the the three numbers is "
    << uno << endl;

// Then you can use uno, dos, and tres in your pow() function to do whatever...

// Write a gereric sort function that takes three doubles and puts them 
// in order where d_one will be >= d_two >= d_three. When you pass
// uno, dos, and tres to this function they are the variables that actually
// will get modified.
 
void sort(double& d_one, double& d_two, double& d_three)
{ 
 // Create three temporary double variables. 
 // Assign d_one to the first, d_two to the second, etc.
 // After sorting the thre temp vars just assign the largest
 // back to d_one, the middle to d_two, and the smallest to d_three.
}
Perion
 

Thread Starter

nanobyte

Joined May 26, 2004
120
Okay Perion. I think I'm really close to getting this program finally working. I have two problems left:
1. How do I go about raising the result of the largest number being raised to the smallest number (the last function -powerhouse2) to the middle number.

2. (In the first function - powerhouse1)When the numbers entered are all equal I want the c-out statement for first function to be "I'll give you" instead of the other c-out statement which is "The result of the largest number raised to the smallest number is", because I have the program set where when all the numbers are equal that powerhouse1 will c-out "1".

As before, the first bit of code is the program and the code about 10-11 lines below that is the header file. I'd appreciate anyone help or input. Take a look at the attachment below. :(
 

Thread Starter

nanobyte

Joined May 26, 2004
120
Alright. I've worked on this program diligently again over the weekend and now have everything working the way I want except for that last function at the bottom(powerhouse2) which is supposed to take the result of the powerhouse1,the second function, and raise it to the middle number. My code is in the attachment below. As before, the first bit of code is the program file and the code about 10-11 lines below that is the header file. I'd appreciate anyone's help or input. Could somebody please help me with this last bit of code. :unsure: :(
 

rauhka

Joined Dec 8, 2004
1
Originally posted by nanobyte@Dec 9 2004, 02:42 AM
Can somebody please help me out with this program!
[post=4024]Quoted post[/post]​
Hey - I'll give it a shot! Right off the bat however, I would really love you to declare all your function prototypes outside of the MyHeader function, and in full. Its much neater, and much better in practice. But I'm sure you don't want to be lectured about style here! :)

eg
Rich (BB code):
//prototypes here!
double larGest(double these, double those, double them);

void myHeader()     //function definition
{
... // not in here!
}
Ok with that out of the way... personally, I would approached this differently, but I will run with what you've got so far.

From powerhouse1 you know your highest, lowest and middle numbers. Where you assign highrise a value I would call powerhouse2 from there. That way you already know tjhe order of your values.

eg.
Rich (BB code):
if (a > b && b < c && c > a || a==c && a > b) 
{
  highrise = pow(c,b);
  cout >> highrise;
  final = powerhouse2(highrise, a); // you know a is the middle value here
  cout >> final;
  
}
  else

.... etc
and then a small rewrite of powerhouse2

Rich (BB code):
//Raises the result of powerhouse1 to the middle number
double powerhouse2(double a, double power)
{
  double final = pow(a, power);
  return final;
}
You just need to fill in the approprate parts of powerhouse1...

Does that help?
 

Perion

Joined Oct 12, 2004
43
Originally posted by nanobyte@Dec 8 2004, 11:42 AM
Can somebody please help me out with this program!
[post=4024]Quoted post[/post]​
Like I said before - I highly recommend sorting the three entered numbers into three variables. Then use the pow(a,b ) function on them however you like.

Sorting three items into decending order is done by the following algorithm where <> contains the final sorted variable list. There's better ways to sort more than three items but when just three are to be sorted you're pretty much limited to decending the "decision tree" - even using clever swaps doesn't help in this case as far as I know.

Rich (BB code):
if a1 < a2 {
  if a1 < a3 {
    if a2 < a3 {
      // sorted order is <a3, a2, a1>
    } else {
      // sorted order is <a2, a3, a1>
    }
  } else {
    // sorted order is <a2, a1, a3>
  }
} else {
  if a2 < a3 {
    if a1 < a3 {
      // sorted order is <a3, a1, a2>
    } else {
      // sorted order is <a1, a3, a2>
    }
  } else {
    // sorted order is <a1, a2, a3>
  }
}
So, some code would look like:
Rich (BB code):
// 
//
// Declare in header file
void sort_three(double a1, double a2, double a3);

// Globals
double largest, middle, smallest;


// somewhere, after getting the input call the sort function
sort_three(uno, dos, tres);

// Then use largest, middle, and smallest in pow(a,b) and its results however you wish.


// Here's the three item sort as a c function:
void sort_three(double a1, double a2, double a3){
  if a1 < a2 {
    if a1 < a3 {
      if a2 < a3 {
        // sorted order is <a3, a2, a1>
        largest  = a3;
        middle   = a2;
        smallest = a1;
      }
      else {
        // sorted order is <a2, a3, a1>
        largest  = a2; 
        middle   = a3;  
        smallest = a1;
      } 
    }
    else {
      // sorted order is <a2, a1, a3>
      largest  = a2;
      middle   = a1;
      smallest = a3;
    }
  }
  else {
    if a2 < a3 {
      if a1 < a3 {
        // sorted order is <a3, a1, a2>
        largest  = a3;
        middle   = a1;
        smallest = a2;
      }
      else {
        // sorted order is <a1, a3, a2>
        largest  = a1;
        middle   = a3;
        smallest = a2; 
      }
    }
    else {
       // sorted order is <a1, a2, a3>
       largest  = a1;
       middle   = a2;
       smallest = a3;
    }
  }
}
Notice that any equal pair is automatically accounted for and just gets left as is. There is no prefered order between equal numbers.

I know this isn't what you wanted but how can you possibly arrive at a satisfactory program where you need to know the order of three items if you don't bite the bullet and sort the items???

BTW - if you can find a better sort algorithm (for three items) out there use it! And let me know what it is too...

Perion
 

Thread Starter

nanobyte

Joined May 26, 2004
120
Ha Ha! I finally got the program working! But I want to add one more function to it called shutDown that will asks the user if he/she wants to run the program again or stop it. Depending on his/her response ('y' or 'Y' for yes and 'n' or 'N' for no) the program will run again or stop. I think you have to use a do/while statement for this but I'm not sure how to approach it. Could somebody show me how to go about doing this function?
 
Top