I have trouble approaching my program assignments. First I write out an algorithm. and work out each part if the program. Then I get nervous. and overwhelmed. with the errors in compliling and such. I have been working on the current assignment several days. And am frustrated and 2nd guessing myself. Feeling very stressed at this point.
advice?
Duoas 1,025 Postaholic Featured Poster
Welcome to the world of programming.
All I can say is: it gets better.
The C++ compiler (especially with the STL) is not a very sympathetic complainer. Show us what you've done: algorithm, code, errors, etc. and we'll help you think your way through it.
Don't feel bad or give up though. You are waaay smarter than that computer (even if it likes to pretend otherwise). :)
clski1973 0 Newbie Poster
I appreciate your thoughts
Currently using diagrams as well. I am trying to approach it as a foreign language. Things make sense in class but once on my own I'm lost. I was doing really well in the beginning.
I work even slower now. the compiler throws me off. I don't continue on anything till all errs are resolved. therfore I havn't gotten very far.
- //Cheryl Slivinski
- //due 11/ /05
- //assignment 8
- /*purpose: Write a C++ program that uses strings, reference parameters
- from Chapter and a while loop with a sentinel value*/
- #include <string>
- #include <iostream>
- using namespace std;
- //3 functions
- void test_score (double& test1, double& test2, double& test3);
- int main ()
- {
- string studentName;
- cout<<"Enter student's name or DONE to quit:";
- getline (cin, studentName);
- void test_score(double& test1,double& test2,double& test3);
- return 0;
- }
- //functions
- void test_score (double& test1, double& test2, double& test3)
- {
- cout<<"Enter 3 test scores:";
- cin >> double test1 >> " " >> double test2 >> " " >> double test3;
- return test_score (double& test1, double& test2, double& test3)
- }}
- "a8slivinski.cpp" 32L, 810C written
- [cgregg14@cscilinux2 cgregg14]$ g++ a8slivinski.cpp
- a8slivinski.cpp: In function `void test_score(double&, double&, double&)':
- a8slivinski.cpp:29: syntax error before `>>' token
- a8slivinski.cpp:31: syntax error before `&' token
- [cgregg14@cscilinux2 cgregg14]$
clski1973 0 Newbie Poster
This problem I started by writing the functions . there are to be 3.
void test_score (as seen on other post)
double average (test 1,test 2, test 3)
{double averagescore;
double totscore;
totscore= test1 +test2 +test3;
averagescore=totscore/3;
returm averagescore;}
void display(studentname, testgrade,averagescore)
{cout << studentname<<endl;
cout<<test1<<test2<<test3<<endl;
cout<<averagescore<<endl;
Duoas 1,025 Postaholic Featured Poster
Your problems are mostly syntactic, not logical (which is good).
When you declare something you give its type. So, for example, you can declare a double as: double test1;
You can likewise declare a function as: void test_score (double& test1, double& test2, double& test3)
You must declare things before you use them so that the compiler knows what type of things they are. According to the above, test1 is a IEEE double floating-point variable, and test_score is a procedure taking three arguments, each one a reference to a double.
That's good. However, once you declare a thing you don't need to remind the compiler what it is when you use it. You did well with the student name: you declared a string variable (studentName) and then used it. You need to do the same thing with test_score:
int main () {
// the student's test scores, obtained using test_score()
double score1, score2, score3;
// the student's name
string studentName;
cout<<"Enter student's name or DONE to quit:";
getline (cin, studentName);
test_score( score1, score2, score3 );
return EXIT_SUCCESS;
}
The type of a thing makes a great deal of difference when programming. In your test_score() function you should remember that its type is "a function taking three references to doubles and returning void (that is, it doesn't return anything)." So it is properly written without a return statement:
void test_score (double& test1, double& test2, double& test3)
{
cout<<"Enter 3 test scores:";
cin >> test1 >> test2 >> test3;
}
You don't need to worry too much about the spaces when using cin.
Your average() and display() functions list the names of each of their arguments, but not the type. Here's a hint:
int add_two_ints( int first, int second ) {
return (first + second);
}
Hope this helps.
clski1973 0 Newbie Poster
Here is a rough idea of what I'm trying to do
*Write a program that will input a student’s name and 3 test grades.
*It will compute the
average grade for the student,
write out the student’s name,
three test scores,
and average grade.
*The program will also compute the average grade for the class. The program must use the following 3 functions in addition to the main function:
1. a void function to read in the three test scores and pass them back to main as reference parameters.
2. a double function that is passed on the parameter list the three test scores, computes the average and returns the average as the value of the double function.
3. a void function that displays the output data (student’s name, 3 test grades and average grade) with appropriate labels.
The program must use a sentinel controlled loop. The program should continue processing students until and uppercase DONE is entered for the student’s name. When DONE is entered, no test scores are to be read or processed. The program is to end.
*Sample Run where the bold data was entered by the user
Enter student’s name or DONE to quit: John Smith
Enter 3 test scores: 85 90 77
Student: John Smith
Three test scores: 85 90 77
Average grade: 84.00
clski1973 0 Newbie Poster
Nice to know that I am thinking logically about all this. I'll have to look up IEEE. So there is some reduntancy in my writing. I thought it looked funny. I get confused on when to have a return statement or not. Voids throw me off.
thank you!
Duoas 1,025 Postaholic Featured Poster
Er, I tend to add a little more information than necessary. IEEE defines the 4-byte and 8-byte floating point number formats used by most minicomputers. In C they are float and double respectively. Don't worry too much about IEEE. Just know that a variable has a specific type, such as int, double, string, bool, void, etc.
THe void type is C and C++'s way of saying that the type of a thing is unknown or undefined. For functions, it means that the function doesn't return anything.
clski1973 0 Newbie Poster
After much time this is what I have, still throwing up errors. ANy hints?
//Cheryl Slivinski
//due 11/ /05
//assignment 8
/*purpose: Write a C++ program that uses strings, reference parameters
from Chapter and a while loop with a sentinel value*/
#include <string>
#include <iostream>
using namespace std;
//3 functions
void test_score (double& test1, double& test2, double& test3);
void display(string studentname,double testgrade,double averagescore);
double average (double test1,double test2,double test3);
int main ()
{
sum=0;
enterstudent=0;
double test1, test2,test3;
string studentName;
while (studentname !=DONE)
{
cout<<"Enter student's name or DONE to quit:";
getline (cin, studentName);
//call function
test_score( test1, test2, test3);
display( studentname, testgrade, averagescore)
return 0;
}
//functions
//prompts for info passes it back to main
void test_score (double& test1, double& test2, double& test3)
{
cout<<"Enter 3 test scores:";
cin >>test1 >>test2>>test3;
}
//average functio
double average (test1,test2, test3)
{
double averagescore;
double totscore;
totscore = test1+test2+test3;
averagescore=totscore/3;
return averagescore;
}
//display function
void display(string studentname,double testgrade,double averagescore)
{
cout<<studentname<<endl;
cout<<test1<" "<<test2<<" "<<test3<<endl;
cout<<averagescore<<endl;
}
Edited by Dani because: Formatting fixed
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
What I'd do is program one step at a time. Using your last description,
Start by writing an empty main()
Then write "1. a void function to read in the three test scores and pass them back to main as reference parameters." Test this by outputting the parameters after the return to main()
. Get it working before moving on.
Next, add "2. a double function that is passed on the parameter list the three test scores, computes the average and returns the average as the value of the double function." Test until ready in the same way.
etc....
Just take your programs a step at a time. Slow and easy,
Duoas 1,025 Postaholic Featured Poster
Here are the errors I am getting from your code:
D:\prog\cc\foo\t>g++ t.cpp
t.cpp: In function `int main()':
t.cpp:19: error: `sum' was not declared in this scope
t.cpp:20: error: `enterstudent' was not declared in this scope
t.cpp:24: error: `studentname' was not declared in this scope
t.cpp:24: error: `DONE' was not declared in this scope
t.cpp:30: error: `testgrade' was not declared in this scope
t.cpp:30: error: `averagescore' was not declared in this scope
t.cpp:32: error: expected `;' before "return"
t.cpp:38: error: a function-definition is not allowed here before '{' token
t.cpp:44: error: initializer expression list treated as compound expression
t.cpp:44: error: expected `,' or `;' before '{' token
t.cpp:53: error: a function-definition is not allowed here before '{' token
t.cpp:57: error: expected `}' at end of input
All those "not declared in this scope" errors mean you are trying to use a variable you didn't define.
The first one, sum, you say: sum = 0;
but you never said: double sum;
You must declare variables before you try to use them.
All the errors after line 32 are due because you forgot to put a } at end of your while loop. Please be careful about how you format and indent your code. Also, when you post, place the code in code tags:
[[I][/I]code[I][/I]]
Code goes here
[[I][/I]/code[I][/I]]
becomes:
Code goes here
Like WaltP says, take things one step at a time.
Hope this helps.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.