Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Would a kind soul please help with C++ assignment?

Would a kind soul please help with C++ assignment?
Thread Tools
earache
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 6, 2005, 11:55 AM
 
Hi,

I'm taking an online course in C++ programming, and of course, it's Windows-centric, and the instructor doesn't know from Xcode. I'm stumped on the following program and was wondering if anyone could help point out my mistake.


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

double num1, num2, num3; //Inputs - Coefficients a, b, c
double disc; // Output - discriminant

void instruction (); //function instruction prototype
double getDiscriminant (double, double, double); //function getDiscriminant prototype
void displayEquation (double, double, double); //function displayEquation prototype

int main ()//main function:

{
instruction (); // call instruction function
cin >> num1, num2, num3; // get input data
getDiscriminant (num1, num2, num3); //call function getDiscriminant

if (disc < 0)
{
cout << "There are no real solutions to the equation: " << endl;
displayEquation (num1, num2, num3); //call function displayEquation
}
else if (disc = 0)
{
double root1, root2;
root1 = (-num2 + sqrt(disc)) / (2 * num1);
root2 = (-num2 - sqrt(disc)) / (2 * num1);

cout << "There are two real solutions, " << root1 << " and " << root2 << ", to the equation: " << endl;
displayEquation (num1, num2, num3); // call function displayEquation
}
else
{
double root1;
root1 = (-num2 + sqrt(disc)) / (2 * num1);
cout << "There is one real solution, " << root1 << ", to the equation: " << endl;
displayEquation (num1, num2, num3);// call function displayEquation
}
return 0; //end of main function
}

//instruction function:
void instruction ()// Display instructions to user
{
cout << "Please enter coefficients a, b, and c of a quadratic equation." << endl;
cout << "The computer will calculate whether there are any solutions to the equation." << endl;
} //end instruction function


double getDiscriminant (double a, double b, double c)// function definition
{
disc = pow(b, 2) – 4 * a * c;
return disc;
} //end getDiscriminant function

void displayEquation (double a, double b, double c)//function definition
{
cout << a << "x^2 + " << b << "x + " << c << endl;
} // end displayEquation function


____________________________________________

I get the following errors on build:

main.cpp:58: error: parse error before numeric constant
main.cpp:58: error: stray '\223' in program
main.cpp:58: error: stray '\200' in program
main.cpp:58: error: stray '\342' in program

which I don't understand - I've never defined a numeric constant.

TIA
     
BLAZE_MkIV
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Jul 6, 2005, 01:15 PM
 
I'd have to guess there are illegal characters at the end of the file. Or did i mis-count and its not 58 lines long?
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jul 6, 2005, 01:55 PM
 
in line 58 you do not declare the variable disc. Do this instead:

return pow(b, 2) - 4 * a * c;
     
zanyterp
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status: Offline
Reply With Quote
Jul 6, 2005, 02:51 PM
 
Originally Posted by samm
in line 58 you do not declare the variable disc. Do this instead:

return pow(b, 2) - 4 * a * c;
the variable disc was declared as global (line 6).


on another note (not sure why the error is occuring, but found another thing to look at)
not sure if you cut & pasted your code from xcode, but around line 24 (not sure if i am counting right) you may want to change the code from

else if (disc = 0)

to else if (disc == 0)


are you copying the code straight from your book (can i ask which you are using)?
some people are like slinkys: they don't do much, but are fun to push down stairs.
     
earache  (op)
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 6, 2005, 04:18 PM
 
Thanks to all who answered. Someone else suggested getting TextWrangler and doing a "Zap Gremlins" on the code, and that got it to build. Then I still had some other errors to deal with, including the else if (disc == 0) mistake.

My textbook is "Problem Solving, Abstraction, and Design using C++" (Friedman, Koffman). So far the only glitch I've encountered was having to do with passing a single character to find as a string - Xcode wouldn't let me do it, forcing me to pass it as a character. My instructor says that Xcode has that wrong, and that " You should be able to pass a single character to find as either a single character ('S') or as a single character stored in a string container ("S")."

I wish I could find out what other things Xcode handles differently than Visual C++ or C++ Builder (the "recommended" compilers).
     
earache  (op)
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 6, 2005, 04:52 PM
 
samm, if I do as you suggest, the result doesn't get passed to the following statements in the main function. I could do

return disc = pow (b, 2) - 4 * a * c;

which works. Is that more efficient than my two original lines:

disc = pow(b, 2) – 4 * a * c;
return disc;

?
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jul 6, 2005, 05:17 PM
 
You can pass a character as a string "S" or character 'S' using XCode. What is the error message that you are getting?

A side note, why don't you use the std::string object since you are writing C++ code? It's much easier than c-strings.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 6, 2005, 05:18 PM
 
That getDiscriminant() function is funky. Why are you both setting a global variable and returning a value that you never use? It looks like you could do away with the global variable and make that part of your main() function read like this:
Code:
double disc = getDiscriminant (num1, num2, num3); //call function getDiscriminant
That seems much more logical to me. I don't see why num1, num2 and num3 are global either.

By the way, could you give me an example of the find code that isn't working for you? I'm having a little trouble understanding exactly what you're doing, and I'm curious where the bug lies.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jul 6, 2005, 05:31 PM
 
Originally Posted by earache
return disc = pow (b, 2) - 4 * a * c;

which works. Is that more efficient than my two original lines:
Your logic is incorrect here. You are returning an integer that indicates if the assignment of pow(b,2) -4 * a * c to the disc global variable succeeded or failed.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 6, 2005, 05:35 PM
 
I think you're mixed up, samm. That assigns the value of the equation "pow (b, 2) - 4 * a * c" to the global variable disc and then returns the value of disc.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jul 6, 2005, 05:51 PM
 
My mistake, sorry. I still question why he is using both a global variable and ignoring the return value of the getDiscriminant() function.
     
earache  (op)
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 6, 2005, 05:58 PM
 
Originally Posted by Chuckit
That getDiscriminant() function is funky. Why are you both setting a global variable and returning a value that you never use? It looks like you could do away with the global variable and make that part of your main() function read like this:
Code:
double disc = getDiscriminant (num1, num2, num3); //call function getDiscriminant
That seems much more logical to me.

I don't see why num1, num2 and num3 are global either.

By the way, could you give me an example of the find code that isn't working for you? I'm having a little trouble understanding exactly what you're doing, and I'm curious where the bug lies.
Well, disc is being used in the main function both as an argument to

if (disc <==> 0) and in the equations that follow:

root1 = (-num2 + sqrt(disc)) / (2 * num1);
root2 = (-num2 - sqrt(disc)) / (2 * num1);

And num1, num2, and num3 are called in the above equations and the displayEquation function as well.

As to the find code that wasn't working it was for a different assignment. I had the following line of code:

blankPosition = sentence.find(" ");

which returned the following errors on build:

main.cpp:48: error: no matching function for call to `std::basic_string, std::allocator >::find()'
main.cpp:48: error: stray '\234' in program
main.cpp:48: error: stray '\200' in program
main.cpp:48: error: stray '\342' in program
main.cpp:48: error: stray '\234' in program
main.cpp:48: error: stray '\200' in program
main.cpp:48: error: stray '\342' in program
basic_string.tcc:705: error: candidates are: typename _Alloc::size_type std::
basic_string<_CharT, _Traits, _Alloc>::find(const _CharT*, typename _Alloc::size_type,
typename _Alloc::size_type) const [with _CharT = char, _Traits = std::
char_traits, _Alloc = std::allocator]
basic_string.tcc:723: error: typename _Alloc::size_type std::
basic_string<_CharT, _Traits, _Alloc>::find(_CharT, typename _Alloc::size_type = 0)
const [with _CharT = char, _Traits = std::char_traits, _Alloc = std::
allocator]
basic_string.h:919: error: typename _Alloc::size_type std::
basic_string<_CharT, _Traits, _Alloc>::find(const std::basic_string<_CharT, _Traits,
_Alloc>&, typename _Alloc::size_type = 0) const [with _CharT = char, _Traits = std::
char_traits, _Alloc = std::allocator]
basic_string.h:923: error: typename _Alloc::size_type std::
basic_string<_CharT, _Traits, _Alloc>::find(const _CharT*, typename _Alloc::size_type
= 0) const [with _CharT = char, _Traits = std::char_traits, _Alloc = std::
allocator]

BUT

the weird thing is that when I loaded the code again and tried with quotations again, it worked!
How strange is that?
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jul 6, 2005, 06:11 PM
 
Originally Posted by earache

blankPosition = sentence.find(" ");
I am assuming that sentence if an object of the C++ Standard Library's string class. If so, the find method works just fine when passing string literals. See this short example and output

Code:
#include <iostream> int main() { std::string foo("hello world"); std::cout << foo << std::endl; std::size_t pos = foo.find("hello"); if (pos != std::string::npos) { std::cout << "Found \"hello\" in string foo" << std::endl; } std::size_t pos2 = foo.find("asdf"); if (pos2 != std::string::npos) { std::cout << "Found \"asdf\" in string foo" << std::endl; } return 0; }
Code:
samm@arnor:~$ g++ -o bar2 bar2.cpp samm@arnor:~$ ./bar2 hello world Found "hello" in string foo
I suggest using a text editor to remove the strange characters from your code and try compiling again. I compiled and ran the above code using gcc 3.3.4 on Debian as well as using gcc4 on my Mac.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 6, 2005, 06:33 PM
 
Originally Posted by earache
Well, disc is being used in the main function both as an argument to

if (disc <==> 0) and in the equations that follow:

root1 = (-num2 + sqrt(disc)) / (2 * num1);
root2 = (-num2 - sqrt(disc)) / (2 * num1);

And num1, num2, and num3 are called in the above equations and the displayEquation function as well.
You can use local variables as arguments just fine.

Anyway, I guess it's not that important if you've got it working all right now. It's just that local variables make a lot more sense there. Using global variables for many things is bad style (and it would be insane to use them like that in a real program — it would be unmanageable), so I figure you may as well learn the right way now
( Last edited by Chuckit; Jul 6, 2005 at 06:49 PM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
earache  (op)
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 6, 2005, 07:40 PM
 
Originally Posted by Chuckit
You can use local variables as arguments just fine.

Anyway, I guess it's not that important if you've got it working all right now. It's just that local variables make a lot more sense there. Using global variables for many things is bad style (and it would be insane to use them like that in a real program — it would be unmanageable), so I figure you may as well learn the right way now
OK, here was the assignment:

================================================== ======

Write a program that finds the real number solutions of a quadratic equation whose coefficients are entered by the user.
If the equation has no real roots it outputs:
There are no real solutions to the equation:
___ x^2 + ___x + ___.
If there are two real roots it outputs:
There are two real solutions, ___ and ___, to the equation:
___ x^2 + ___x + ___.
If there is one real root it outputs:
There is one real solution, ____, to the equation:
___ x^2 + ___x + ___.

The program contains three functions:
q a function to give instructions to the user
q a function that returns the discriminant of the equation
q a function that outputs the equation in the form ___ x^2 + ___x + ___

================================================== ==

The input variables are the coefficients a, b, c. These need to be obtained in the main function and passed to the function that returns the discrimant and the function that outputs the equation, as well as being arguments in the equations that find the roots (in the main function). Are you saying that I should not declare the input variables as global variables but instead make them local to the main function? Is that more efficient?

And variable disc needs to be returned from the getDiscriminant function to both the if else arguments and the equations that find the roots. So doesn't that variable need to be declared globally to be visible in both the main function and the getDiscriminant function?

The textbook, at this point, doesn't mention anything about the relative efficiency of global vs. local variables.

BTW, I know there were other errors in the original code I posted, since fixed.
     
earache  (op)
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 6, 2005, 07:44 PM
 
Originally Posted by samm
I am assuming that sentence if an object of the C++ Standard Library's string class. If so, the find method works just fine when passing string literals. See this short example and output

Code:
#include <iostream> int main() { std::string foo("hello world"); std::cout << foo << std::endl; std::size_t pos = foo.find("hello"); if (pos != std::string::npos) { std::cout << "Found \"hello\" in string foo" << std::endl; } std::size_t pos2 = foo.find("asdf"); if (pos2 != std::string::npos) { std::cout << "Found \"asdf\" in string foo" << std::endl; } return 0; }
Code:
samm@arnor:~$ g++ -o bar2 bar2.cpp samm@arnor:~$ ./bar2 hello world Found "hello" in string foo
I suggest using a text editor to remove the strange characters from your code and try compiling again. I compiled and ran the above code using gcc 3.3.4 on Debian as well as using gcc4 on my Mac.
Just curious - why so many instances of the string "std" in your code? My textbook teaches to include

#include <string>
using namespace std;

after which I don't need to use the std statement again.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 6, 2005, 07:58 PM
 
Originally Posted by earache
The input variables are the coefficients a, b, c. These need to be obtained in the main function and passed to the function that returns the discrimant and the function that outputs the equation, as well as being arguments in the equations that find the roots (in the main function). Are you saying that I should not declare the input variables as global variables but instead make them local to the main function? Is that more efficient?
It's not more efficient (that is, your program will not run better because of it), but it's much better code. Like I said, if you want to ignore it now, you can, but it'll get pounded into your head over the rest of the course anyway.

Originally Posted by earache
And variable disc needs to be returned from the getDiscriminant function to both the if else arguments and the equations that find the roots. So doesn't that variable need to be declared globally to be visible in both the main function and the getDiscriminant function?
You don't need to reference disc in the getDiscriminant function. The getDiscriminant function returns the value for disc. You can assign disc to that in your main function.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jul 7, 2005, 09:08 AM
 
Originally Posted by earache
Just curious - why so many instances of the string "std" in your code? My textbook teaches to include

#include <string>
using namespace std;

after which I don't need to use the std statement again.
Get a better textbook. The "using namespace std;" is a kludge, it brings everything from the std namespace into scope. You can run into problems if you start naming variables using names found in the std namespace. A better solution would be:

#include <string>
using std::string;

Then you can create string objects just like build-in types.

http://www.parashift.com/c++-faq-lit....html#faq-27.5
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 7, 2005, 10:10 AM
 
I'm not sure ugly, std::-filled code is much better...
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jul 7, 2005, 10:57 AM
 
Originally Posted by Chuckit
I'm not sure ugly, std::-filled code is much better...
Then you can use the using directive to include which parts of the std namespace you would like to use in that scope. Like I said before, and linked to the C++ FAQ, lumping the entire std namespace in your scope can and will cause problems in the future. Obviously it works fine for short programs, but in larger projects it becomes an issue.

Code:
#include <iostream> using std::string; using std::cout; using std::endl; using std::size_t; int main() { string foo("hello world"); cout << foo << endl; size_t pos = foo.find("hello"); if (pos != string::npos) { cout << "Found \"hello\" in string foo" << endl; } size_t pos2 = foo.find("asdf"); if (pos2 != string::npos) { cout << "Found \"asdf\" in string foo" << endl; } return 0; }
     
   
Thread Tools
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 08:44 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,