Hello everyone, I've been browsing this forum for small while and have gotten some great information out of it through searching so I thank you very much for that.

Currently though I have a C++ program I have to write for class that inputs the full name of the student, then it asks for their lab point, their homework points and their test points.

I'm going to make it out of a hundred, so I'll have the lab worth 35 points, hw worth 15 points, and test worth 50 points.

Then it has to take the total value and give it a letter grade based on these requirements:
100-80 A
79 - 50 B
49 - 45 C
44 and below F

Here is what I have so far, I've got the total grade in and the name input(except the last name doesn't show for some reason) so now I just have to get the program to spit out a letter grade using if else statements. Need help ASAP!!!! this is due in an hour :(

#include <iostream>

float lab, homework, test, total;
char line[75];

main()
{
cout<< "Enter Students' lab points from 0-35 "<< "\n";
cin>> lab;

cout<< "Enter Students' homework points from 0-15 "<< "\n";
cin>> homework;

cout<< "Enter Students' test points from 0-50 "<< "\n";
cin>> test;
total = lab + homework + test;

cout<< "Enter students' full name: "<< "\n";
cin>> line;

cout<< "Students' name is "<< line << "\n";
cout<< "Final grade is "<< total << "\n";
return(0);
}

Recommended Answers

All 36 Replies

Greetings,

The if-else statement is normally used to express decisions, where the else part is usually optional. Since an if simply tests the numeric value of an expression, coding shortcuts are possible:

if (expression)
// instead of
if (expression != 0)

Example 1.1: Using coding shortcuts

There is an ambiguity when an else is omitted from a nested if sequence, since the else part of an if-else is optional.

if (a < 0)
	if (b < c)
		r = b;
	else
		r = c;

Example 1.2: Ambiguity between control flow statements

The construction of the if-else syntax is simple:

if (expression)
	statement 1
else
	statement 2

The else-if construction is similar, though it evaluates all expressions in order; and if any expression is true, the statement associated with it is executed and terminates the whole chain:

if (expression)
	statment
else if (expression)
	statement
else if (expression)
	statement
else
	statement

Example 1.3: Using the Else-If syntax

So to do accomplish your task, you could do something like the following:

if (total >= 80 && total <= 100)	// 80 between 100
	statement;
else if (total >= 50 && total <= 79)	// 50 between 79
	statement;
else if (total >= 45 && total <= 49)	// 45 between 49
	statement;
else	// probably an F grade here
	statement;

Code 1.1: Using if-else in application

If you have multiple statements withing one if, you must group the declarations together into a compund statement using braces ({ }):

if (expression) {	// multi-computation
	statement 1;
	statement 2;
}else	// single computation
	statement 3;

Example 1.4: Grouping declarations

I hope this helps, and if you have any further questions please feel free to ask.


- Stack Overflow

Thanks a BUNCH stack.

That was a great explaination and I'm glad to be welcomed like that, you don't normally see someone help a new person (or at least put in that much effort).

I'm on my second assignment now which I have to write a program that will ask the user for one of three choices.

for example:

Press c to calculate the area of a circle, t for a triangle, r for a rectangle, or q for quit.

Then the program comes and asks if you typed in c

"What is the radius of the circle?"
you input the radius then it spits out the area of the circle then loops back to the main question until you hit q to quit. I have the concept down of what I want to do with it but I know there is more to it. Here is what I've got so far.

#include<iostream>
using namespace std;
float answer;

int main()
{
char shape;
double height, width, base, radius;

cout<< "Press r for rectangle, t for triangle, or c for circle " << "\n";
cin>> shape;

if(shape=='r'||shape=='R')
{
double height, width, answer;
cout<< "Enter the Height:";
cin>> height;
cout<< "Enter the Width:";
cin>> width;
answer = height * width;
cout<< "The area is: " << answer << "\n";
}

elseif(shape=='t'||shape=='T')
{
double height, base;
cout<< "Enter the height:";
cin>> height;
cout<< "Enter the base length:";
cin>> base;
answer = base * height * .5;
cout<< "The area is: " << answer << "\n";
}

elseif(shape=='c'||shape='C')
{
double radius;
cout<< "Enter the Radius:";
cin>> radius;
answer = radius * 3.1419;
cout<< The area is: " << answer << "\n";
}

return(0)
}

// I'm sick of trying to figure this out :)


Thanks for any help you can give.

I'm on my second assignment now which I have to write a program that will ask the user for one of three choices.

for example:

Press c to calculate the area of a circle, t for a triangle, r for a rectangle, or q for quit.

Then the program comes and asks if you typed in c

"What is the radius of the circle?"
you input the radius then it spits out the area of the circle then loops back to the main question until you hit q to quit. I have the concept down of what I want to do with it but I know there is more to it. Here is what I've got so far.

Throw what you've got in a loop, add another possibility to check for (and fix some minor typos).

#include<iostream>
 using namespace std;
 float answer;
 
 int main()
 {
    for(;;)
    {
 	  char shape;
 	  double height, width, base, radius;
 
 	  cout<< "Press r for rectangle, t for triangle, or c for circle, q to quit\n";
 	  cin>> shape;
 
 	  if(shape == 'q' || shape == 'Q')
 	  {
 		 break;
 	  }
 	  else if ( shape=='r'||shape=='R' )
 	  {
 		 double height, width, answer;
 		 cout<< "Enter the Height:";
 		 cin>> height;
 		 cout<< "Enter the Width:";
 		 cin>> width;
 		 answer = height * width;
 		 cout<< "The area is: " << answer << "\n";
 	  }
 
 	  else if(shape=='t'||shape=='T')
 	  {
 		 double height, base;
 		 cout<< "Enter the height:";
 		 cin>> height;
 		 cout<< "Enter the base length:";
 		 cin>> base;
 		 answer = base * height * .5;
 		 cout<< "The area is: " << answer << "\n";
 	  }
 
 	  else if(shape=='c'||shape=='C')
 	  {
 		 double radius;
 		 cout<< "Enter the Radius:";
 		 cin>> radius;
 		 answer = radius * 3.1419;
 		 cout<< "The area is: " << answer << "\n";
 	  }
    }
    return 0;
 }
 
 /* my output
 Press r for rectangle, t for triangle, or c for circle, q to quit
 r
 Enter the Height:2
 Enter the Width:4
 The area is: 8
 Press r for rectangle, t for triangle, or c for circle, q to quit
 c
 Enter the Radius:6
 The area is: 18.8514
 Press r for rectangle, t for triangle, or c for circle, q to quit
 t
 Enter the height:2
 Enter the base length:9
 The area is: 9
 Press r for rectangle, t for triangle, or c for circle, q to quit
 q
 */

Thanks a BUNCH stack.

That was a great explaination and I'm glad to be welcomed like that, you don't normally see someone help a new person (or at least put in that much effort).

» Glad to be of assistance.

I'm on my second assignment now which I have to write a program that will ask the user for one of three choices... Here is what I've got so far.

» I did notice a few typographical errors in your provided source. Though, lets take the time and fix them up. Once this is done, your program should work just fine.

Firstly, lets look at our else-if calls. In your example, you may receive undeclared identifier errors because the call else and if are two different expression decisions, though are in construction with each other:

// Your approach
elseif(shape...
// Correct approach
else if(shape...

The C langauge can be a pain, though it wouldn't be very useful if we could use our syntactical styles whenever we wanted to. Once you modify your program with the new syntax, you may recieve less errors. Though, this isn't just done yet.

The last cout also has an error:

cout<< The area is: " << answer << "\n";

It may not be easy to catch, but there is no starting quotation for your sentence. This will leave the compiler confused, and not knowing when the string literal "The area is: " is supposed to start or end. It is simple to fix this, just as a " infront of 'The':

cout<< "The area is: " << answer << "\n";

Another small thing is the return value. Every statement must end with a semi-colon, telling the compiler where to break. Loop expressions and other can do away with depending on how you are writing it.

return(0);

I colored in red of what should be there. One last thing is your last else-if call has a syntatical error:

else if(shape=='c'||shape='C')

Not easy to catch, but failing to use the logical operator == may cause your program to always believe shape is 'C'. The compiler is asking shape to equal 'C' rather than if it compares to 'C'. Changing the is quite simple too:

else if(shape=='c'||shape=='C')

Once that is cleaned up, your program should execute. If you have further questions about this procedure, please feel free to ask.


Hope this helps,
- Stack Overflow

Wow, I'm simply amazed by the help you get on here. I should be paying you guys the 9k a year to teach me this stuff instead of some teacher than I've honestly learned VERY little from. I'm pretty much doing this all on my own.

I'll be writing all of my programs and coming on here to let me know what I'm doing wrong and I can gaurantee I'll learn the coding process much faster than sitting in class.

Do you guys also mind if I email my finished hw to you to at least check over?

Thanks so much again guys!

Andy

Do you guys also mind if I email my finished hw to you to at least check over?

I believe the preferred practice is to post here and have code critiqued here. That way, someone's corrections may themselves be corrected. Also, others that are just reading or are searching the board can benefit from the posts as well.

hey, I could use a little help too. I need to write a program that asks for a series of integers one at a time. when 0 is entered I need to make the program display the following: the number of ints entered. The average of them. the largest and smallest ones. and the difference between the largest and smallest. The area that I need help with the most would be finding the number of integers entered.

thanx

hey, I could use a little help too.

Beginning a new thread would have been more appropriate.

I need to write a program that asks for a series of integers one at a time. when 0 is entered I need to make the program display the following: the number of ints entered. The average of them. the largest and smallest ones. and the difference between the largest and smallest.

Post your code. For an average, you will most likely need an array.

The area that I need help with the most would be finding the number of integers entered.

Increment a counter in the loop that prompts for data.

Thanks Dave, sounds good I'll just continue to post on here! :)

I have to write a program that inputs 12 temperatures and the difference of the current and the one preceding it, and I have to write it to a file "out.dat", I have tried writing it using cin, and the ifstream and ofstream, and I can only get it to output 7 temperatures, can you give me some advice or something to help me. this is my first time writing a pragram using files. thanks.

Well im getting desperate. I'm in class right now and have to finish an assignment by the end of class which is an hour and 45 minutes away. If anyone happens to see this post please help. Ive got another assignment i need to finish and here it is:

It should look like this:

This program tracks the weather. Would you like to enter some data? y

Enter atmosphereic condition (s= sunny, c= cloudy, r= rain): c

Enter high temperature in degrees Fahrenheit: 78

Do you want to enter another day of data? n (have to reloop if yes)

Total number of days entered = 1

There were 0 sunny, 1 cloudy, and 0 rainy days with an average temperature of 78 degrees Fahrenheit.

Would you like to do some more data? n

And here is the code i have so far:

#include<iostream>
using namespace std;
float answer,condition;

int main()
{
  for(;;)
    {

      char condition, yesno;
      double temp, average;

      cout<< "This program tracks the weather.  Would you like to enter some data? " << "\n";
      cin>> condition;

      if(conditon == 'n' || condition == 'N')
        {
           break;
        }

       else if(condition == 's' || condition == 'S')
        {
           double temp;
           cout<< "Enter the high temperate in degrees Fahrenheit: " << "\n";
           cin>> temp;
           cout<< "Do you want to enter another day of data? " << "\n";
           cin>> yesno;
           if(yesno == 'n' || yesno == 'N')
             {
               cout<< "Total number of days entered = 1 " << "\n";
               cout<< "Temperature of: " << temp << "degrees Fahrenheit" << "\n";
               cout<< "There were 1 sunny, 0 cloudy, and 0 rainy day(s) with an average temperature of " << temp << "\n";
            }
           else if(yesno == 'y' || yesno == 'Y')
             {
               return(main)
             }

       else if(conditon == 'c' || conditon == 'C')
         {
           double temp;
           cout<< "Enter the high temperate in degrees Fahrenheit: " << "\n";
           cin>> temp;
           cout<< "Do you want to enter another day of data? " << "\n";
           cin>> yesno;
           if(yesno == 'n' || yesno == 'N')
             {
               cout<< "Total number of days entered = 2 " << "\n";
               cout<< "Temperature of: " << temp << "degrees Fahrenheit " << "\n";
               cout<< "There were 1 sunny, 1 cloudy, and 0 rainy day(s) with an average temperature of " << temp << "\n";
             }
            else if(yesno == 'y' || yesno == 'Y')
              {
               return(main)
                 }

           }
           else if(conditon == 'r' || conditon 'R')
             {
               double temp;
               cout<< "Enter the high temperate in degrees Fahrenheit: " << "\n";
               cin>> temp;
               cout<< "Do you want to enter another day of data? " << "\n";
               cin>> yesno;
               if(yesno == 'n' || yesno == 'N')
                 {
                   cout<< "Total number of days entered = 1 " << "\n";
                   cout<< "Temperature of: " << temp << "degrees Fahrenheit " << "\n";
                   cout<< "There were 1 sunny, 0 cloudy, and 0 rainy day(s) with an average temperature of " << temp << "\n";
             }
           else if(yesno == 'y' || yesno == 'Y')
             {
               return(main)
             }
}
}

Greetings sid3ways s13,

I overviewed your source, and found a few errors which are easy to fix. Let's take a look at them one step at a time, and find out why this program doesn't compile:

1: Some of your if statements have a few typographical errors:

if(conditon == 'n' || condition == 'N')

As seen there, condition and conditon are spelled differently. Missing one i can cause your compiler to generate errors. Just add the i to condition as seen below:
> condition

Please note, more of your if statements have this error when calling condition. For simplicity, copy one that is spelled properly, and paste them over all your current calls for condition.


2: Your last else-if statement contains a syntactical error.

else if(condition == 'r' || condition 'R')

Easily overlooked, your condition is not checking if 'R' is of the select. Rather, there is no == operator anywhere near the seperation of the two statements.


3: Return calls. Your return calls for main may also generate warnings if not errors.

return(main)

Syntax errors as (1) no space between return and main() (2) main is not a variable and (3) there is no leading semicolon to break the statement.
In other words, your statement should be somewhere near this analogy:

return main();

main() is a function, with no arguments. You would call on main like you would any other function.


If you have further questions, please feel free to ask.


Hope this helps,
- Stack Overflow

excellent advice Stack, I have the program running now but it seems how it wrote it, if you press n to not enter data it just breaks out fine, but if you press y it proceeds to ask the first question of the high temp.

I forgot to put in the:

Enter the atmospheric condition (s=sunny and so on). So how would I go about having when the user selects Y to then go into a look that would ask for the one of three days like sunny cloudy or rain.

also ive found out that when it asks if you want to enter another day of data it just quits out when you press y as well.

And lastly ive got to come up with a way to be able to count how many of each day types were inputted like 3 rainy and 2 sunny or something. As well as take an average of the temps and output them....thanks a bunch again.

I'm on AIM as RCellViper to chat if you have the ability! :)

In response to,

So how would I go about having when the user selects Y to then go into a look that would ask for the one of three days like sunny cloudy or rain.

This would be quite simple. Let's take a look at the existing code:

if(yesno == 'n' || yesno == 'N') {
	cout<< "Total number of days entered = 1 " << "\n";
	cout<< "Temperature of: " << temp << "degrees Fahrenheit " << "\n";
	cout<< "There were 1 sunny, 0 cloudy, and 0 rainy day(s) with an average temperature of " << temp << "\n";
}else if(yesno == 'y' || yesno == 'Y') {
	return main();
}

Alright, as we see, if we wanted to add further data for another day - it is somewhat impossible. We can take two approaches to accomplish your task.

  • Use the goto statement
  • Split your program into different functions

The second is most recommended, though I will explain the goto.

Using goto
Explaining the goto statement lets look at our modifications:

Above the line:

cout<< "Enter the high temperate in degrees Fahrenheit: " << "\n";

Lets put in our code thats bold:

[b]more:[/b]
cout<< "Enter the high temperate in degrees Fahrenheit: " << "\n";

Simple. C provides the infinitely-abusable goto statement. The goto statement labels to branch to. more here is our scope of a label in the entire function.

So instead of doing:

{
	return main();
}

All you have to do is:

{
	goto more;
}

Now another problem we may run into is trying to keep your last inputted data and add it to your existing variable. To make this easier, lets create a new double data-type variable called keep:

char condition, yesno;
double temp, average, [b]keep[/b] = 0;

Now, whenever you do:

cin >> temp;

Just add this after:

keep += temp;

Also, instead of calling on temp in your cout statements, use keep instead:

cout<< "There were 1 sunny, 0 cloudy, and 0 rainy day(s) with an average temperature of " << [b]keep[/b] << "\n";

And so forth.


Hope this helps,
- Stack Overflow

Ok got another program that is due soon I'm still having problems with. All it has to it is this:

It will ask for a number, you type in 7 lets say, it will then consecutively add every number including 7 up and give an output of those numbers added. Then it just has to ask you if you want to run the program again. Here's what I have but I keep getting syntax errors it is making me mad :mad: I can't figure it out.

Greetings,

The few syntax errors are easy to fix. Let's go through them one step at a time.

Point A
You include iostream which is a Standard ISO C++ header. Standard library facilities are declared in namespace std in headers without a .h suffix. We do know that iostream.h is an old style method of including std C++ headers, and in opposite to iostream you don't have to declare that you're using the std namespace.

It is sometimes recommended to use:

#include <iostream>
using namespace std;

Point B
Your variable initialization is setup incorrectly. If you wanted to initialize a variable to zero, all you would have to do is "int var = 0;". For example:

int main() {
	int abc = 0, def = 1;

	return 0;
}

If that makes sense.

Point C
If you want to get the sum total of your value, by consecutively adding every number including # and up, it will take a simple for loop. You have done well already, though there is a simple issue.

For example, let's take 5. When you retrieve the value from the program, we will want our total to equal 5. After that, we will send our loop from 1 until the end of value and add i to total. This is how it would work:

--------------
total = 5

total += 1 | 6
total += 2 | 8
total += 3 | 11
total += 4 | 15
total += 5 | 20
--------------

Our answer is 20. Here is a look of how this can be done simply:

total = value;

for (i = 1; i <= value; i++)
	total += i;

Hope this helps.

Conclusion
I hope this information has been useful to you.


- Stack Overflow

I have to write a program that inputs 12 temperatures and the difference of the current and the one preceding it, and I have to write it to a file "out.dat", I have tried writing it using cin, and the ifstream and ofstream, and I can only get it to output 7 temperatures, can you give me some advice or something to help me. this is my first time writing a pragram using files. thanks.

Tomas,

If you've not already done so, (I noticed you had 2 posts under your belt) please start a new thread for your issue. Dave Sinkula already asked another user (the b) to do the same thing-- please follow suit.

Thank you again Stack, can't thank you enough. I've got another assignment coming up soon I'm in the process of trying to complete. I'll post it up for you gods to let me know what I'm doing wrong and right ;)

All that have helped I can't thank enough!

Ok onto the next assignment, I am to take the shapes program I wrote earlier in one of my posts (the one that calculated area of a triangle and so on if you entered the dimensions). I have to know take the program and insert for and if loops into the program as well as condition statements. I'm not exactly sure what he was talking about I guess I should have asked further. But I do remember he wanted us to use for and while commands. This is the current program which Dave modified and used a for command in his help he offered.

I'm kinda confused on what I would need to add in to use for / if / and while statements? Maybe you can shed some light and I'll go from there.

#include<iostream>
using namespace std;
float answer;

int main()
{
for(;;)
{
char shape;
double height, width, base, radius;

cout<< "Press r for rectangle, t for triangle, or c for circle, q to quit\n";
cin>> shape;

if(shape == 'q' || shape == 'Q')
{
break;
}
else if ( shape=='r'||shape=='R' )
{
double height, width, answer;
cout<< "Enter the Height:";
cin>> height;
cout<< "Enter the Width:";
cin>> width;
answer = height * width;
cout<< "The area is: " << answer << "\n";
}

else if(shape=='t'||shape=='T')
{
double height, base;
cout<< "Enter the height:";
cin>> height;
cout<< "Enter the base length:";
cin>> base;
answer = base * height * .5;
cout<< "The area is: " << answer << "\n";
}

else if(shape=='c'||shape=='C')
{
double radius;
cout<< "Enter the Radius:";
cin>> radius;
answer = radius * 3.1419;
cout<< "The area is: " << answer << "\n";
}
}
return 0;
}

Thinking about it more I remember he said we had to have something about having seperate programs, almost like if you press c for shape it goes out of the loop, enters the program that calculates the area of the circle then goes back into the main loop. Hopefully that makes some sense? :)

Greetings,

This is possible to do. You can just call inside your if statement to a function you manually create. You could call it calculateRect(). What it will do is take your variable answer, and send the memory address to it. This will help because when we do so, we can write to its data and affect it anywhere in the program.

For instance, if we were to send a variable to a function and change it, it would only change there. Though if we send the memory address, we can change the data from the address sent using the unary dereferencing operator '*'.

else if (shape == 'r' || shape == 'R') {
	calculateRect(&answer); // Send memory address of answer using &
	cout<< "The area is: " << answer << "\n";
}

Here we just ask another function to do our work. For example, this is the type dirty work that can make your main() look cleaner if you split your program into little pieces:

void calculateRect(double *answer) {
	double height, width;
	cout << "Enter the Height: ";
	cin >> height;
	cout << "Enter the Width: ";
	cin >> width;
	*answer = height * width;
}

If you have further questions, please feel free to ask.


- Stack Overflow

Ok here is the next assignment, I think I've got the sum part down, now I have to do the average part with an inline function?

-Write a program that allows the user to enter numbers into an array and returns the sum and average of the elements

-Plan the program by creating a flow chart

-Sum the values using a recursive function

-Average the values using the sum function in an inline function

-Program must not accept more than 100, so array size of 100 or less.

#include <iostream.h>

#define MAX 30

//functions

void Prompt(); // welcome message
int Read(int []); // read array of integers from standard input
void PrintA(int [], int); // print array on standard output
void Print(int); // print result
int Sum(int [], int); // computer sum, recursively

main()
{
int a[MAX]; //elements to be added
int n; //and their number

int s; //result

Prompt();
n=Read(a);
PrintA(a,n);

s = Sum(a,n);

Print(s);

}//end main

//***********
//
// Prompt
//
//***********

void Prompt()
{
cout << "A Recursive Function for adding an array of numbers" << endl;
}// end Prompt

//***********
//
// Read
//
//***********

int Read(int a[])
{
cout <<
"Please enter the numbers you want to add. End with CTRL-D" << endl;
int n=0;

while (cin && n <MAX)
cin >> a[n++];

return(n--);
} // end Read


//*************
//
// PrintA
//
//*************

void PrintA(int a[], int n)
{
cout << "n= " << n << endl;

for (int i = 0; i<n ; i++)
cout << a << " ";
cout << endl;
} // end PrintA


//*************
//
// Print
// Prints the results
//*************

void Print(int s)
{
cout << "The sum is " << s << endl;
} //end Print

//*************
//
// Sum
//
//*************

int Sum(int data[], int n)
{
int val;

cout << "Recursive call of sum for n= " << n << endl;

if (n == 0)
{
cout << "Returning from recursive call of sum, n-0" << endl;
return(0);
}

else
{
val = data[n-1] + Sum(data, n-1);
cout << "Returning from recursive call of sum, n= " << n << endl;
return(val);
}

}//end sum

Greetings,

Creating an inline function to return the average of a group of numbers is not difficult. In this case, we will take all the values of the array and average them. The algorithm to average is not hard:

loop through all arrays
	take [b]n[/b] amount [i]and[/i] add it to a single variable
divide sum to max elements

A good reason to inline is that you can sometimes speed up your program by inlining the right function. Instead of calling the function every time it is invoked, the compiler will replace the function call with a copy of the function body. If it's a small function which gets called a lot, this can sometimes speed things up. On the other hand, the compiler will copy the entire function body every time the function is called, if it is a large function (more than three or four lines), inlining can increase the size of your executable program significantly.

Our inline function produces three lines of code that computes heavily, or at least more than initializing variables. We must state that C++ implements the macro as an inline function, which is a true function in every sense. Any behavior you expect from an ordinary function, you get from an inline function. The only difference is that an inline function is expanded in place, like a preprocessor macro, so the overhead of the function call is eliminated.

To start off we know of two common variables to send to our inline function; the array, and the element size:

inline float average(int array[], int size) {

The catch is we return a floating-type variable. The average of 20 numbers can't be a whole number. Now onto the actual average code itself it works quite simply:

for (i = 0; i < size; ++i)
	av += array[i];
return (av / size);

This does not include the two variable initialization of av and i. Simple, i is a standard integer, and av will be initialized to 0.0f as it will be a float. I added the next line of code after you call Print() in main():

cout << "The average is " << average(a, n) << endl;

Seems to work like a charm.

Hope this helps,
- Stack Overfow

New program here, we had to add the power function, the exponent function and the square function. I've done most of it but I have no idea what do put for the power function I'm totally lost.

The program just continues to ask you what you want to do to the number for example add or subtract or square or take an exponent to some power of the previously totaled number. It worked fine with the simple instructions but when I added the 3 extras it has quit working :(

#include <iostream>
#include "math.h"
#define SQR(x) ((x)*(x))
#define REC(x) ((1)/(x))
#define E(x) (power(2.71828,x))
#define P(x,y) (power(x,y))


int result;     //the result of the calculations
char oper_char; //operator the user specified
int value;      //value specified after the operator
float exp;


using namespace std;


int main()
{
result = 0;


//loop forever (or until break reached)
while (true) {
std::cout << "Enter operator and number: ";


std::cin >> oper_char >> value;


if ((oper_char == 'q') || (oper_char == 'Q'))
break;


switch (oper_char) {
case '+':
result += value;
break;


case '-':
result -= value;
break;


case '*':
result *= value;
break;


case '^':
result = SQR(value);
break;


case 'e':
result = E(value);
break;


case 'p':
std::cout << "Enter the exponent ";
std::cin >> exp;
result = P(value)^exp;
break;


case '|':
result = REC(value);
break;


case '/':
if (value == 0) {
std::cout << "Error: Divide by zero\n";
std::cout << "   operation ignored\n";
} else
result /= value;
break;
case 'h':
case 'H':
std::cout << "Operator   Meaning\n";
std::cout << "  +        Add\n";
std::cout << "  -        Subtract\n";
std::cout << "  *        Multiply\n";
std::cout << "  /        Divide\n";
std::cout << "  e        Exponent\n";
std::cout << "  ^        Square\n";
std::cout << "  |        Reciprical\n";
std::cout << "  p        Power\n";
continue;
default:
std::cout << "Unknown operator " << oper_char << '\n';
continue;
}
std::cout << "result: " << result << '\n';
}
return(0);
}

EDIT: Oh yeah, the reciprical function just returns a zero everytime??? Can anyone figure that part out as well?

commented: Use code tags. +0

I've done most of it but I have no idea what do put for the power function I'm totally lost.

pow?

Oh yeah, the reciprical function just returns a zero everytime??? Can anyone figure that part out as well?

You're doing integer division. If you want floating point math, using floating point variables.

Here is the newest assignment, thanks for your previous input Dave.

I have to write a mailing list program that uses a structure for the personal information. The structure shuold include individual fields for:

index(integer)
first name
last name
middle initla
street
city
street
zip (5 digit no.)
flags (integer hex value initialized at 0x00)

The structure should be assigned to an array called ourgroup with room for 100 entries. The program should accept data entry and allow for the display of all of the entires, or an individual entry by searching index, last name or type (flags).

Data entry should ask questions and set the appropriate bits in the flags variable.

Heres the sorry attempt I have so far, I don't really understand structures so if someone can shed some light on structures in English I'd be more than appreciative! :)

struct info {
int index;
char fname;
char lname;
char minitial;
float street;
char city;
int zip;
float flags;
}
struct ourgroup.info


count=1
while (true)
{
cout << "Enter data?";
cin >> ans;

if (ans || 'y')
break;
ourgroup[count].index = count;
cout << "First name ";
cin >> ourgroup[count].fname;

count=count + 1

I have to write a mailing list program that uses a structure for the personal information. The structure shuold include individual fields for:

index(integer)
first name
last name
middle initla
street
city
street
zip (5 digit no.)
flags (integer hex value initialized at 0x00)

The structure should be assigned to an array called ourgroup with room for 100 entries. The program should accept data entry and allow for the display of all of the entires, or an individual entry by searching index, last name or type (flags).

Data entry should ask questions and set the appropriate bits in the flags variable.

Heres the sorry attempt I have so far, I don't really understand structures so if someone can shed some light on structures in English I'd be more than appreciative! :)

struct info {
int index;
char fname; // perhaps you want a std::string or a char array?
char lname; // perhaps you want a std::string or a char array?
char minitial;
float street; // perhaps you want a std::string or a char array?
char city; // perhaps you want a std::string or a char array?
int zip;
float flags; // this is already spelled out
}
struct ourgroup.info // should be struct info ourgroup[100];


count=1 // did you forget the semicolon? and that arrays begin at 0?
while (true)
{
cout << "Enter data?";
cin >> ans; // doing anything with this?

if (ans || 'y') // what?
break;
ourgroup[count].index = count;
cout << "First name ";
cin >> ourgroup[count].fname;

count=count + 1

Use

tags around your code. Post actual code that you've run through a compiler within them.

Do you need help obtaining strings? Do you need help obtaining ints? This assignment only asks these two things, but with more involved syntax that you seem to have an initial grasp of.

Ok i still haven't figured out this assignment. If someone wants to assist me one on one with this assignment I'll pay them 50 bucks through paypal. The assignment is now my final and it is due tomorrow afternoon. So I'll be on instant messenger if someone can give me personal help with writing this. I HAVE to get it done and I'm desperate, and I know there are many very intelligent people on here ;)

AIM name is : RCellViper I'll be online almost all night starting in about 20 mins. Thanks!

Ok i still haven't figured out this assignment. If someone wants to assist me one on one with this assignment I'll pay them 50 bucks through paypal. The assignment is now my final and it is due tomorrow afternoon. So I'll be on instant messenger if someone can give me personal help with writing this. I HAVE to get it done and I'm desperate, and I know there are many very intelligent people on here ;)

AIM name is : RCellViper I'll be online almost all night starting in about 20 mins. Thanks!

Erm... We really don't allow solicitations to do homework for pay here on the forums. Just give it a little bit, and somebody will probably help. The time you spend waiting here could be spent trying to review your notes to ensure you completely understand the assignment you've been given.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.