473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Having trouble with my code for hw assignment

4
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write a function qualityPoints that inputs a student's average and returns 4 if a student's average is 90 - 100, 3 if the average is 80 - 89, 2 if the average is 70 - 79, 1 if the average is 60 - 69 and 0 if the average is lower than 60.

Look tried
put where the error?

#include <iostream.h>
void main ()
{
int s;

if ((s>=90) && (s <=100))
{
return 4;
}
else if((s>=80) && (s<=89))
{
return 3;
}
else if ((s>=70) && (s<=79))
{
return 2;
}
else if ((s>=60) && (s<=69))
{
return 1;
}

}
Jul 27 '06 #1
6 3596
D_C
293 100+
You never input s.

How about
Expand|Select|Wrap|Line Numbers
  1. cout << "Enter student's grade (0-100)" << endl;
  2. cin >> s;
Then use the logic. Of course, a faster way would be
Expand|Select|Wrap|Line Numbers
  1. if((s >= 0) && (s <= 100))
  2. {
  3.   s -= 60;
  4.   s /= 10;
  5.   if(s == 5) // only needed when a student gets 100%
  6.     s--;
  7.   cout << s << endl;
  8. }
Jul 27 '06 #2
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write a function qualityPoints that inputs a student's average and returns 4 if a student's average is 90 - 100, 3 if the average is 80 - 89, 2 if the average is 70 - 79, 1 if the average is 60 - 69 and 0 if the average is lower than 60.

Look tried
put where the error?

#include <iostream.h>
void main ()
{
int s;

if ((s>=90) && (s <=100))
{
return 4;
}
else if((s>=80) && (s<=89))
{
return 3;
}
else if ((s>=70) && (s<=79))
{
return 2;
}
else if ((s>=60) && (s<=69))
{
return 1;
}

}

Hi,

First thing is that u r trying to return from main( ); which is having void return type.

Instead of using so many if, else conditions u can try this

if(s>59)
return( (((s-60)/10)+1) );
else
return 0;
Jul 28 '06 #3
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. if((s >= 0) && (s <= 100))
  2. {
  3.   s -= 60;
  4.   s /= 10;
  5.   if(s == 5) // only needed when a student gets 100%
  6.     s--;
  7.   cout << s << endl;
  8. }
You have an error and return the wrong values show in the following table

Expand|Select|Wrap|Line Numbers
  1.                        Return Value
  2. Score (range)     Required     From Code
  3. 60 - 69               1            0
  4. 70 - 79               2            1
  5. 80 - 89               3            2
  6. 90 - 99               4            3
  7. 100                   4            4
  8.  
Your s==5 case never happens.
Jul 28 '06 #4
Banfa
9,065 Expert Mod 8TB
First thing is that u r trying to return from main( ); which is having void return type.
In fact the problem is with the declaration of main.

in C main must be declared as

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[ ])
  2. {
  3.     /*program-statements */
  4. }
  5.  
This works for C++ to however when using C++ you also have the option of declaring main as

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     /*program-statements */
  4. }
  5.  
In all cases main must return int. Not returning int from main invokes undefined behaviour (which is not a good thing).

Unless you have any specific return value in mind then you should return

EXIT_SUCCESS

or

EXIT_FAILURE

from main. The are portable return values and should be defined in stdlib.h
Jul 28 '06 #5
dna5122
12
I think you guys are giving kookai too much information too early in the game. If the assignment is to write that function then a ladder of if-else statements is probably a good thing. If the assignment is to write a gradebook application then the condensed arithmetic is more impressive.

It should be noted that not returning an int does not always result in undefined behavior. Windows is fine with void main() { return; }, but I know other operating systems aren't.
Jul 29 '06 #6
mkane
1
Here's a solution that's probably close to what your teacher was expecting. The changes I made to your code:
1. put your logic in a function
2. added return 0;
3. called the function from main.

[CODE}
#include <iostream.h>

int qualityPoints()
{
int s;

cout << "Enter student's grade (0-100)" << endl;
cin >> s;

if ((s >= 90) && (s <= 100))
{
return 4;
}
else if ((s >= 80) && (s <= 89))
{
return 3;
}
else if ((s >= 70) && (s <= 79))
{
return 2;
}
else if ((s >= 60) && (s <= 69))
{
return 1;
}
return 0;
}


void main ()
{
int points = qualityPoints();
cout << "Points: " << points << endl;
return;
}

[/code]
Oct 20 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Exits Funnel | last post by:
Hello, I've been tasked with porting some C++ code from Windows to Linux. The following excerpt is giving me trouble: //BEGIN CODE #include <string> class TempTestBase_t {
1
by: REH | last post by:
As I posted last week, I updated my C++ library to include a variant record class. While using it in some code, I'm finding it would be convenient to have a method that would basically say to the...
7
by: JustSomeGuy | last post by:
I have two classes, class english and class metric. I seem to be having dificulty getting this to compile properly... I'm using powerpc-apple-darwin8-g++-4.0.0 There are 5 files, main.cpp...
2
by: Felix Collins | last post by:
Hi, I'm trying to assign a resource to a task in MS Project by using the example from MSDN for VB... "Use the Add method to add an Assignment object to the Assignments collection. The...
7
by: | last post by:
I fail to understand why that the memory allocated in the void create(int **matrix) does not remain. I passed the address of matrix so shouldn't it still have the allocated memory when it returns...
7
by: Charles Krug | last post by:
List: I've a module that's not doing what I expect. My guess is that I don't quite understand the scoping rules the way I should. I have an object that's costly to create. My thought was to...
1
by: Thundercleese | last post by:
Hello everyone. I've been working on an assignment which is described here http://scidiv.bcc.ctc.edu/mg/211/programs/program5.htm I've gotten to a point where it works for most cases but...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
3
by: Howard Swope | last post by:
Greetings: C++ CLR .Net 2 I have a ref class that I have created that wraps an unmanaged pointer. It acts like a smart pointer for reference counted objects for a particular library I am...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.