Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 09-23-2003, 05:40 PM   #1 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
Help Please (Need help by Monday 29th of September!)

I cannot get this program to work! Would someone please tell me what I'm doing wrong, and perferably correct my problem!?

[SHELL]
#include <iostream.h>
#include <stdlib.h>

void getValues();
void findLowest();
void calcAverage();

int main()
{

getValues();
findLowest();
calcAverage();

system("PAUSE");
return 0;
}

void getValues()
{
int a,b,c,d,e;
cout << "Please enter 5 test scores (0-100): ";
cin >> a >> b >> c >> d >> e;
if (a>100 || a<0 || b>100 || b<0 || c>100 || c<0 || d>100 || d<0 || e>100 || e<0)
{
cout << "The requirment was between 0 and 100" << endl;
}
}

void findLowest(int a, int b, int c, int d, int e)
{
int& aa=a;
int& bb=b;
int& cc=c;
int& dd=d;
int& ee=e;

int lowest;
int& low=lowest;
if (aa<bb && aa<cc && aa<dd && aa<ee)
{
lowest=aa;
}
if (bb<aa && bb<cc && bb<dd && bb<ee)
{
lowest=bb;
}
if (cc<aa && cc<bb && cc<dd && cc<ee)
{
lowest=cc;
}
if (dd<bb && dd<cc && dd<aa && dd<ee)
{
lowest=dd;
}
if (ee<bb && ee<cc && ee<dd && ee<aa)
{
lowest=ee;
}
cout << "The Lowest Score Was: " << lowest << endl;
}

void calcAverage(int a, int b, int c, int d, int e,int low)
{
int avg;
avg = (((a+b+c+d+e)-low)/4))
cout << "The Average score was: " << avg << endl;
}

[/SHELL]
CoW is offline   Reply With Quote
Old 09-23-2003, 06:14 PM   #2 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
ok what kind of errors are you getting?

something i notice off the bat
1. you should declare your variables to store test scores in the main function and pass them to your later functions.

2. int& ? are you trying to declare a int pointer if so the syntax is int*
& means address of.

3. I would check to see the lowest score by testing the first 2 and then take the lowest for the next. I would also use an array to store the integers.

Code:
int current = 0;
int next = 1;

for( int i; i < ARRAY_SIZE-2; i++){
   if ( score[next] < score[current] ){
      current = next;
      next++;
    }
}
when that code finishes executing then current will contain the index of the lowest score. you could also fill the array so that it is sorted from lowest to highest.
palin is offline   Reply With Quote
Old 09-24-2003, 07:47 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I have no idea what your current skillset is, nor do I know what the requirements are, but here is something that works, without comprimising your style too much (I hope).

Code:
#include <iostream.h>

void getValues();
void findLowest();
void calcAverage();

int a,b,c,d,e,lowest; //Will hold user input and calculated lowest input.

int main()
{
	getValues();
	findLowest();
	calcAverage();
	return 0;
}

void getValues()
{	
	cout << "Please enter 5 test scores (0-100): ";
	cin >> a >> b >> c >> d >> e;
	if (a>100 || a<0 || b>100 || b<0 || c>100 || c<0 || d>100 || d<0 || e>100 || e<0)
	{
		cout<<endl<<"WARNING, INVALID DATA:"<<endl<<
			"Entered data was NOT between 0 and 100, scores below are INVALID!"<<endl<<endl;
	}
}

void findLowest()
{
	if (a<b && a<c && a<d && a<e){
		lowest=a;
	}

	if (b<a && b<c && b<d && b<e){
		lowest=b;
	}

	if (c<a && c<b && c<d && c<e){
		lowest=c;
	}

	if (d<b && d<c && d<a && d<e){
		lowest=d;
	}
	
	if (e<b && e<c && e<d && e<a){
		lowest=e;
	}
	
	cout << "The Lowest Score Was: " << lowest << endl;
}

void calcAverage()
{
	int avg;
	avg = ((a+b+c+d+e)-lowest)/4;
	cout << "The Average score was: " << avg << endl;
}
__________________
Valmont is offline   Reply With Quote
Old 09-25-2003, 11:41 AM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
work? it's not even close to compiling.
try writing one function at a time, compiling, and testing it. don't go on until you've gotten it to work.

your first problem is dealing with scopes. for example, if you declare variables a, b, c, etc. in function getValues(), those variables no longer exist once that function has ended. so, that function isn't very useful for getting data, since the data it gets disappears once it's over. perhaps global variables would work better.

what's this supposed to do?
int& aa=a;
that's junk. don't do that.

try to fix it up, and come back to us with if you have any specific questions about why something doesn't work.
joe_bruin is offline   Reply With Quote
Old 09-28-2003, 12:19 AM   #5 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
Quote:
Originally posted by joe_bruin
work? it's not even close to compiling.
try writing one function at a time, compiling, and testing it. don't go on until you've gotten it to work.

your first problem is dealing with scopes. for example, if you declare variables a, b, c, etc. in function getValues(), those variables no longer exist once that function has ended. so, that function isn't very useful for getting data, since the data it gets disappears once it's over. perhaps global variables would work better.

what's this supposed to do?
int& aa=a;
that's junk. don't do that.

try to fix it up, and come back to us with if you have any specific questions about why something doesn't work.
its some **** my professor was telling us about called "reference variables" and the coding on this site showed that int& newname=name; is the correct coding for it... and that should allow variables to be accessed from outside of the function its located in...
CoW is offline   Reply With Quote
Old 09-28-2003, 08:46 AM   #6 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
In all my reading and school its probably not wise to do that even if it works it will make the code hard to read and maintain. pass what you need in each function it will make the purpose more clear.
palin is offline   Reply With Quote
Old 09-28-2003, 05:24 PM   #7 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
if you could edit the code so that would work i would appreciate it, becuase all ive been taught in passing information between functions is the reference variables thing...
CoW is offline   Reply With Quote
Old 09-28-2003, 06:13 PM   #8 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
YAY I GOT IT TO WORK! THANKS FOR THE HELP GUYS!
CoW is offline   Reply With Quote
Old 10-02-2003, 05:30 AM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
YOU got it to work???
__________________
Valmont is offline   Reply With Quote
Old 10-02-2003, 05:32 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally posted by CoW
if you could edit the code so that would work i would appreciate it, ...
This part I did.
__________________
Valmont is offline   Reply With Quote
Old 10-02-2003, 05:35 AM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Just a friendly heads up...

You are not ready for references yet I think.
Learn to pass by value first. And always learn to use char(s) and string(s)very well.

*Edited later*
I wonder, do you have a decent C++ learning book? Just a thought that passed my mind. Try Sam's. I believe it's affordable, but don't quote me.

Good luck!
__________________
Valmont is offline   Reply With Quote
Old 10-02-2003, 09:31 AM   #12 (permalink)
.pakmon.
Guest
 
Posts: n/a
::: Note: this is raw code because it's been a while since i've done functions ::: Doing this at work because I was bored. I can't remember VOID functions for the life of me... (pass by value? pass by reference?... it's been a while.) I'd also like to know what it would look like with void functions.

#include <iostream>
#include <cmath>
using namespace std;

//Function Prototypes
// void findLowest();
// void calcAverage();

int main()
{
double testScore1 = 0.0;
double testScore2 = 0.0;
double testScore3 = 0.0;
double testScore4 = 0.0;
double testScore5 = 0.0;
double average = 0.0;

cout << "Enter test score 1: ";
cin >> testScore1;
cout << "Enter test score 2: ";
cin >> testScore2;
cout << "Enter test score 3: ";
cin >> testScore3;
cout << "Enter test score 4: ";
cin >> testScore4;
cout << "Enter test score 5: ";
cin >> testScore5;

if (testScore1 > 100 || testScore1 < 0 || testScore2 > 100 || testScore2 < 0 || testScore3>100 || testScore3 < 0 || testScore4 > 100 || testScore4 < 0 || testScore5 > 100 || testScore5 < 0)
{
cout<<"WARNING, INVALID DATA:" << endl;
}
else
{
//double findLowest(double score);
if(testScore1 < testScore2 && testScore1 < testScore3 && testScore1 < testScore4 && testScore1 < testScore5)
{
cout << "Test Score: " << testScore1 << " is the lowest test score." << endl;
}
else
if(testScore2 < testScore1 && testScore2 < testScore3 && testScore2 < testScore4 && testScore2 < testScore5)
{
cout << "Test Score: " << testScore2 << " is the lowest test score." << endl;
}
else
if(testScore3 < testScore1 && testScore3 < testScore2 && testScore3 < testScore4 && testScore3 < testScore5)
{
cout << "Test Score: " << testScore3 << " is the lowest test score." << endl;
}
else
if(testScore4 < testScore1 && testScore4 < testScore2 && testScore4 < testScore3 && testScore4 < testScore5)
{
cout << "Test Score: " << testScore4 << " is the lowest test score." << endl;
}
else
{
cout << "Test Score: " << testScore5 << " is the lowest test score" << endl;
}

}

// calcAverage();
average = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5) / 4;
cout << "The average of all 5 test is: " << average << endl;


return 0;
}
  Reply With Quote
Old 10-02-2003, 09:38 AM   #13 (permalink)
.pakmon.
Guest
 
Posts: n/a
ack, n/m

++++++++++++++++++
here's my $.02. it's still the same, but made small changes so it works on my compiler and because i was bored. i only made 3 instead of 5 because of lack of time during the time when i was coding this, so if you want to add 2 more, go for it... just don't forget to edit in 3 areas, prototypes, main, void functions...

also added a lil function that reports a letter grade to the average given.

peace.
pak
++++++++++++++++++

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//function prototype
void getValues(double &num1, double &num2, double &num3);
void lowestScore(double testa, double testb, double testc);
void calcAverage(double testscore1, double testscore2, double testscore3, double &average);
void getName(string &name1, string &name2);
void letterGrade(double score1, double score2, double score3, string &letter);

int main()
{
double score1 = 0.0;
double score2 = 0.0;
double score3 = 0.0;
double average = 0.0;
string firstname = "";
string lastname = "";
string letter = "";

//get name
getName(firstname, lastname);

//get values
getValues(score1, score2, score3);

//calculate average
calcAverage(score1, score2, score3, average);

//display lowest score
lowestScore(score1, score2, score3);

//display the average
cout << endl << "The average of all 3 test scores for " << firstname << " " << lastname << " is: " << average << endl;

//display letter grade
letterGrade(score1, score2, score3, letter);

return 0;
}

//get name
void getName(string &name1, string &name2)
{
cout << "Enter First Name: ";
cin >> name1;
cout << "Enter Last Name: ";
cin >> name2;
}

// find the lowest test score
void lowestScore(double testa, double testb, double testc)
{
if(testa < testb && testa < testc)
{
cout << endl << "" << testa << " is the lowest test score." << endl;
}
else
if(testb < testa && testb < testc)
{
cout << endl << "" << testb << " is the lowest test score." << endl;
}
else
{
cout << endl << "" << testc << " is the lowest test score." << endl;
}
}

//get values function
void getValues(double &num1, double &num2, double &num3)
{
cout << "Enter the first test score: ";
cin >> num1;
cout << "Enter the second test score: ";
cin >> num2;
cout << "Enter the third test score: ";
cin >> num3;

if(num1 < 0 || num1 > 100 || num2 < 0 || num2 > 100 || num3 < 0 || num3 > 100)
{
cout << endl << "Invalid Entry. Data can't be less than 0 or over 100. Try again." << endl;

cout << endl << "Enter the first test score: ";
cin >> num1;
cout << "Enter the second test score: ";
cin >> num2;
cout << "Enter the third test score: ";
cin >> num3;

}
else
{
}
}


//calculate average
void calcAverage(double testscore1, double testscore2, double testscore3, double &average)
{
average = (testscore1 + testscore2 + testscore3) / 3;
}

//letter grade
void letterGrade(double score1, double score2, double score3, string &letter)
{
if(score1, score2, score3 < 100 && score1,score2,score3 >=90)
{
cout << endl << "Letter Grade is A." << endl;
}
else
if(score1, score2, score3 < 90 && score1,score2,score3 >=80)
{
cout << endl << "Letter Grade is B." << endl;
}
else
if(score1, score2, score3 < 80 && score1,score2,score3 >= 70)
{
cout << endl << "Letter Grade is C." << endl;
}
else
if(score1, score2, score3 < 70 && score1,score2,score3 >=60)
{
cout << endl << "Letter Grade is D." << endl;
}
else
{
cout << endl << "Letter Grade is F." << endl;
}
}
  Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:29 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting