Skip to main content
0 online

Help for C++. It's been ages. LOCKED

theremin by thereminOG 2002 · Jul 19, 2006 · 275 views · ·

Locked Thread

This thread is locked. New replies are not allowed.

#include

using namespace std;

void getScore(int& score);
void printGrade(int cScore);

int main()
{
int courseScore;

cout << "Line 1: Based on the course score, this "
<< "program computes the course grade."

getScore(courseScore);
printGrade(courseScore);

return 0;

}

void getScore(int& score)
{
cout << "Line 4: Enter the course score: ";
cin >> score;
cout << endl << "Line 6: Course score is "
<< score << endl;
}

void printGrade(int cScore)
{
cout << "Line 7: Your grade for the course is ";

if (cScore >= 90)
cout << "A." << endl;
else if (cScore >= 80)
cout << "B." << endl;
else if (cScore >= 70)
cout << "C." << endl;
else if (cScore >= 60)
cout << "D." << endl;
else
cout << "F." << endl;
}

I want to rewrite the function printGrade as a value-returning function so that it computes & returns the course grade and output the course grade in function main.

51 Comments

yay yayOG 2004 ·

hey how about that C++ huh? Good stuff ya?

yay yayOG 2004 ·

yeah good stuff man we should get together and code it out some time

yay yayOG 2004 ·

aw bro i cant wait

yay yayOG 2004 ·

yeah bro i got some sick modified stream libraries

theremin thereminOG 2002 ·

So I have to figure out a way to convert my Java mortgage calculator to C++.

Any suggestions?

Doesn't have to be GUI though.

theremin thereminOG 2002 ·

Nevermind on my request for help on this one. I started to get it rolling finally.

I'll post what I did next week maybe.

I'm doing a little spin on my original Java version.

theremin thereminOG 2002 ·

ended up making a monthly remaining balance mortgage program. would be nnice if it worked tho. I was on a roll until I tried to add the feature of showing the remaining balance each month until the entire loan was paid off.

anyway, here's what I did... corrections appreciated

#include
#include
#include

//Program figures out a mortgage repayment schedule
//Showing the user how much of the balance on the loan remains after each month

using namespace std;

int main(int argc, char *argv[])
{

char exit;

double amount;
int term;
double rate;
double result;

double principal;
double monthly_interest;

double towards_bal;
double new_bal;

double new_interest_amount;

int count;

//To escape the program Hit x
cout << "To exit Hit x";
cin >> exit;
cout << endl;

//Program runs unless x is hit
while (exit != 'x');
{
//User enters Amount without commas
cout << "Enter full amount of your home mortgage loan: $";
cin >> amount;

cout << "Enter the interest rate in percentage: %";
cin >> rate;

cout << "How much can you pay per month in dollars: $";
cin >> principal;

monthly_interest = (rate / 12) * amount * 0.01;
cout << "Your monthly interest is: $" << monthly_interest << endl;

towards_bal = principal - monthly_interest;
cout << "Contribution towards balance of principle would be: $" << towards_bal;

cout << endl;
cout << endl;

new_bal = amount - towards_bal;

//After the first month is figured out, a do while loop is entered in order
//to display the remaining balances for each month until the entire loan
//is pay off, in other words until the balance equals zero
do
{

outData.open("a:\\prog.out"); //open output file
outData << endl;
outData << "Balance: $" << new_bal << endl;

cout << endl;

cout << "Your new balance as of the following month would be: $";
cout << new_bal;

cout << endl;

new_interest_amount = monthly_interest = (rate / 12) * new_bal * 0.01;
cout << "Based on this payment next month's interest amount would be: $";
cout << new_interest_amount;

cout << endl;

towards_bal = principal - new_interest_amount;
cout << "Contribution towards balance of principle would be: $" << towards_bal;

new_bal = new_bal - towards_bal;
}
while (new_bal != 0);

cout << endl;
}

outData.close();

}
system("PAUSE");
return EXIT_SUCCESS;
}

theremin thereminOG 2002 ·

Well, despite my crippled state at the moment, I managed to get most of my programs done today. Now, I just have 2 left to write. The tough ones tho.

theremin thereminOG 2002 ·

This following program compiles w/o errors, but it's not running when I try to run it. The screen just flinches.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{


int count;
int alpha[5];

alpha[0] = 5;

for (count = 1; count < 5; count++)
{
alpha[count] = 5 * count + 10;
alpha[count - 1] = alpha[count] - 4;

}


cout << "List elements: ";

for (count = 0; count < 5; count++)
cout << alpha[count] << " ";
return 0;

}

D
dgiaimoOG 2003 ·

Should't you have something following those "#include"s?

theremin thereminOG 2002 ·

Yes of course. I do. They got omitted in the post somehow I guess. Weird.

So that's not the answer. Sorry. hrm

iwz iwz ·

oof, this isn't for #1 is it? what is your goal in that first for loop? it's creating some pretty weird numbers in the array

theremin thereminOG 2002 ·

Totally separate program. Should've said that. Sorry.

Funnily enough, I don't even know the goal of it.

Just wanna know what it outputs. But it's not running.
And there's not even any errors in compiling.

iwz iwz ·

i dunno, it looks fine to me, but i don't know C, nor do i have a C compiler.. hehe

D
dgiaimoOG 2003 ·

How can you know Java, but not C?

theremin thereminOG 2002 ·

Actually Java was a little easier for me

iwz iwz ·

I learned Java on the job.

Here's how my programming learning has gone so far:

Logo (hehe) -> GW-BASIC -> QuickBasic -> HTML -> VBScript -> JavaScript -> AppleScript -> ColdFusion -> Java. And I'm currently learning Ruby.

Of those, I learned AppleScript, ColdFusion and Java on the job.

I really would love to learn C and C++, but I don't really have an application for it.

theremin thereminOG 2002 ·

BASIC's my favourite. (no sarcasm) Ahh the simple times.

iwz iwz ·

haha why did i include HTML in there

theremin thereminOG 2002 ·

It counts

D
dgiaimoOG 2003 ·

Interesting. For me it went:

Logo (Yes, me too) -> BASIC -> C -> C++ -> Java -> Perl (kinda) -> C# (kinda) -> PL/SQL

C and C++ are actually really good to learn so that you can play around with low level stuff like memory management. Sometimes when you only use Java you can forget about all the stuff going on behind the scenes.

yay yayOG 2004 ·

Oo Oo i want to play

Hmm well, I didn't learn in a specific order

theremin thereminOG 2002 ·

For me:

HTML - 1996/97, Visual BASIC - 1997, BASIC - 1997 (Gw & Q. What's the difference anyway?) yeh I learned kinda backwards, C++ - 1998, Visual BASIC - 1999, C - 2000, JavaScript - Early 2000s forget exactly, PERL early 2000s, some CSS somewhere around here, SQL 2003, Java 2006, and now I'm back to relearning C++ as well as CSS and XML.

I know I seem intelligent enough, but I used to be better at this stuff. I'll probably wind up doing something in Real Estate or teaching English.

theremin thereminOG 2002 ·

I'm using the Bloodshed Dev-C++ http://www.bloodshed.net/

deanh77 deanh77Founder ·

why don't you try changing this:
for (count = 0; count < 5; count++)
cout << alpha[count] << " ";

to:

for (count = 0; count < 5; count++) {
cout << alpha[count] << " ";
}

I know in java you can do the first, but I'm not sure if it works the same in C, anyway, give it a shot. (My C is really really rusty)

D
dgiaimoOG 2003 ·

I just copied it verbatim and compiled it, and it ran just fine. You must have something set up weird on your machine.

theremin thereminOG 2002 ·

May you copy and post the output ? Thanks

D
dgiaimoOG 2003 ·

List elements: 11 16 21 26 30

iwz iwz ·

yeah i think that's what i got when i did it on paper

theremin thereminOG 2002 ·

Thanks

yay yayOG 2004 ·

The screen is closing before you can see the output, run in a command line window or figure out how to put a key capture at the end (like getch() or _getch())

yay yayOG 2004 ·

*bump*
I was right? I see system("pause");

I think I've seen this problem/solution nearly 50 times.

theremin thereminOG 2002 ·

isn't getch only in C?

didn't know C++ has the command

gonna flip thru my book

D
dgiaimoOG 2003 ·

Anything (almost) in C is also in C++.

yay yayOG 2004 ·

Yeah the new standard even puts these into the namespace by making new standard headers prefixed with c, like cmath, and cstdlib, cstdio

theremin thereminOG 2002 ·

Ok. That's done with. Here's 2 others I need help with that I'm posting in the midst of trying to figure it out in the meantime.

#1
an array called "alpha" with 50 components - type is double

initialise array so that the 1st 25 components are equal to the square of the index variable and the last 25 are equal to 3 times the index variable. output the array so 10 elements per line are printed.

#2
use a 2-D array to store the highest & lowest temperatures for each month of the year. Output the average high, avg. low, as well as the highest & lowest temps of the year.

It consists of 5 functions:

getData - reads & stores data in the 2D array

averageHigh - Calculates & returns the average high temps of the year obviously

averageLow - self-explanatory

indexHighTemp - returns the index of the highest high temperature in the array

indexLowTemp - just the opposite of the indexHighTemp function of course

If you can offer any help, thanks.

iwz iwz ·

what are you having trouble with on #1?

theremin thereminOG 2002 ·

The biggest thing I need help with on is how to print out 10 elements per line

yay yayOG 2004 ·

its just an array with 50 elements printed out in 10's

theremin thereminOG 2002 ·

10 elements per line need to be outputted. But, How do I do that?

I thought maybe a nested for loop like this one

for (index = 0; index < 50; index++)
for (item = 0; item < 10; item++)
cout << alpha[index] << " " << endl;

But, is that how?

Probably nothing like that, right?

I doubt myself.

yay yayOG 2004 ·

There ya go. But the first loop should not be increased by 1, but 10, otherwise you'll have 40 more sets of 10 printing out then expected

D
dgiaimoOG 2003 ·

Not quite. What you want is more like:

for (i = 0; i < 5; i++)
{

for (j = 0; j < 10; j++)
{
cout << alpha[10*i + j] << " ";
}

cout << endl;
}

theremin thereminOG 2002 ·

Here's what I did (Doesn't work properly tho)

#include
#include

using namespace std;

int main(int argc, char *argv[])
{

double alpha[50];
int index;

for (index = 0; index < 25; index++)

alpha[index] = index^2;

if (index == 10)
cout << alpha[index] << endl;
else if
(index == 20)
cout << alpha[index] << endl;
else cout << alpha[index] << " ";



for(index = 25; index < 50; index++)
alpha[index] = index * 3;

if (index == 30)
cout << alpha[index] << endl;
else if
(index == 40)
cout << alpha[index] << endl;
else if
(index == 50)
cout << alpha[index] << endl;
else cout << alpha[index] << " ";




system("PAUSE");
return EXIT_SUCCESS;
}

yay yayOG 2004 ·

First of all ^ does not do what you think it does, its an exclusive or logical operator, not power, you want to include cmath and use pow for that.

Second, I'd be amazed if you could even tell me in english what that code does. Any good compiler would complain that index at lines x y and z will never be 10, 20, 30, 40, or 50, a clue right there that your printing is notpart of your looping calculation.

First, do your calculations which looks like you want to square the index and store it into the first 25 elements, then multiples of 3 on the index for the remaining 25 (two seperate loops)

THEN, do your print out code, which we already gave you tips on in other comments

D
dgiaimoOG 2003 ·

Maybe if you told us what you were having trouble on we could be more useful. I could easily do the problems for you, but I fail to see how that would help you.

yay yayOG 2004 ·

lol yeah i know, do you want us to do it for you, or learn how to do it?

theremin thereminOG 2002 ·

Do it for me

theremin thereminOG 2002 ·

This won't compile... :(

#include

using namespace std;

void getScore(int& score);
char convertScoreToGrade(int cScore);

int main()
{
int courseScore;

cout << "Line 1: Based on the course score, this "
<< "program computes the course grade.";

getScore(courseScore);
convertScoreToGrade(int cScore);

cout << "Line 7: Your grade for the course is ";

return 0;

}

void getScore(int& score)
{
cout << "Line 4: Enter the course score: ";
cin >> score;
cout << endl << "Line 6: Course score is "
<< score << endl;
}

char convertScoreToGrade(int cScore)
{ if (cScore >= 90)
return 'A';
else if (cScore >= 80)
return 'B';
else if (cScore >= 70)
return 'C';
else if (cScore >= 60)
return 'D';
else
return 'F';
}

yay yayOG 2004 ·

Why doesn't it compile? What's the compiler saying?

Two reasons it won't work:
1. You don't keep the return of convertScoreToGrade() to a char variable.
2. You don't print out the returned value of convertScoreToGrade()

iwz iwz ·

yeah, the most important thing when asking for help is to post the compiler errors.

D
dgiaimoOG 2003 ·

The easiest way to do this is to change printGrade so that it returns a char. Then just return the appropriate letter. For example you could use a function like this:

// Renamed printGrade to better convey
// the semantics of the function
char convertScoreToGrade(int cScore)
{
if (cScore >= 90)
return 'A';
else if (cScore >= 80)
return 'B';
else if (cScore >= 70)
return 'C';
else if (cScore >= 60)
return 'D';
else
return 'F';
}

Now you just need to move all the printing that was in printGrade into the main function and call this function to get the letter grade.

Welcome Back to eZabel

It's been a while. Here's what's new.

eZabel Lore

A complete history of our community — stats, Hall of Fame, legendary threads, and more.

View the Lore →

Everything Preserved

All 225,969 pieces of content from 2000–2014 are here — forums, messages, journals, photos, polls, and events.

💎

Gems

Spot something you love — a legendary comment, a classic thread, a great photo? Log in and click the diamond icon to mark it as a Gem. Add a note about why it's special. The best stuff surfaces on the Gems page.