943,398 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13543
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 21st, 2004
1

Need help writing a program

Expand Post »
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);
}
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
sid3ways s13 is offline Offline
15 posts
since Sep 2004
Sep 21st, 2004
1

Re: Need help ASAP writing a program

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Sep 23rd, 2004
0

Re: Need help writing a program

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

Re: Need help writing a program

Quote originally posted by sid3ways s13 ...
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
 */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 23rd, 2004
0

Re: Need help writing a program

Quote originally posted by sid3ways s13 ...
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.

Quote originally posted by sid3ways s13 ...
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:

C++ Syntax (Toggle Plain Text)
  1. cout<< The area is: " << answer << "\n";
  2.  
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':

C++ Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Sep 24th, 2004
0

Re: Need help writing a program

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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
sid3ways s13 is offline Offline
15 posts
since Sep 2004
Sep 24th, 2004
0

Re: Need help writing a program

Quote originally posted by sid3ways s13 ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 24th, 2004
0

Re: Need help writing a program

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
Reputation Points: 12
Solved Threads: 0
Light Poster
the b is offline Offline
42 posts
since Sep 2004
Sep 24th, 2004
0

Re: Need help writing a program

Quote originally posted by the b ...
hey, I could use a little help too.
Beginning a new thread would have been more appropriate.

Quote originally posted by the b ...
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.

Quote originally posted by the b ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 25th, 2004
0

Re: Need help writing a program

Thanks Dave, sounds good I'll just continue to post on here!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
sid3ways s13 is offline Offline
15 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: guide me please
Next Thread in C++ Forum Timeline: question about rand() function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC