954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help writing a program

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

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);
}

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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 nestedif 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 theif-else syntax is simple:

if (expression)
	statement 1
else
	statement 2


Theelse-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 oneif, 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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

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
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.

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 
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
 */
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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 lastcout 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 lastelse-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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

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

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 
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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

the b
Light Poster
42 posts since Sep 2004
Reputation Points: 12
Solved Threads: 0
 
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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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 Lopez
Newbie Poster
21 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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
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)
}
}
}

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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 onei 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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

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! :)

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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:

<strong>more:</strong>
cout<< "Enter the high temperate in degrees Fahrenheit: " << "\n";


Simple. C provides the infinitely-abusablegoto 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 newdouble data-type variable called keep:

char condition, yesno;
double temp, average, <strong>keep</strong> = 0;


Now, whenever you do:

cin >> temp;

Just add this after:

keep += temp;


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

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


And so forth.


Hope this helps,
-Stack Overflow

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

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.

Attachments program.JPG 22.29KB
sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 
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.

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

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!

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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
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;
}

sid3ways s13
Newbie Poster
15 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You