RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 9849 | Replies: 36
Reply
Join Date: Sep 2004
Posts: 15
Reputation: sid3ways s13 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
sid3ways s13 sid3ways s13 is offline Offline
Newbie Poster

Need help writing a program

  #1  
Sep 21st, 2004
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);
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Need help ASAP writing a program

  #2  
Sep 21st, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Sep 2004
Posts: 15
Reputation: sid3ways s13 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
sid3ways s13 sid3ways s13 is offline Offline
Newbie Poster

Re: Need help writing a program

  #3  
Sep 23rd, 2004
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.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,763
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help writing a program

  #4  
Sep 23rd, 2004
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
 */
 
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Need help writing a program

  #5  
Sep 23rd, 2004
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.

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:

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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Sep 2004
Posts: 15
Reputation: sid3ways s13 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
sid3ways s13 sid3ways s13 is offline Offline
Newbie Poster

Re: Need help writing a program

  #6  
Sep 24th, 2004
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
Reply With Quote  
Join Date: Apr 2004
Posts: 3,763
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help writing a program

  #7  
Sep 24th, 2004
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.
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Sep 2004
Posts: 42
Reputation: the b is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
the b the b is offline Offline
Light Poster

Re: Need help writing a program

  #8  
Sep 24th, 2004
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
Reply With Quote  
Join Date: Apr 2004
Posts: 3,763
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help writing a program

  #9  
Sep 24th, 2004
Originally Posted by the b
hey, I could use a little help too.
Beginning a new thread would have been more appropriate.

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.

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.
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Sep 2004
Posts: 15
Reputation: sid3ways s13 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
sid3ways s13 sid3ways s13 is offline Offline
Newbie Poster

Re: Need help writing a program

  #10  
Sep 25th, 2004
Thanks Dave, sounds good I'll just continue to post on here!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:18 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC