When Even The Posted Solution Isn't Working

  • Thread starter Heisenberg.
  • Start date
  • Tags
    even
In summary, the conversation is about a person trying to learn C++ and encountering errors while trying to run an example from a guide book. They share their code and ask for help in identifying and fixing the errors. The expert summarizes the conversation and provides a fixed version of the code with explanations for the errors.
  • #1
Heisenberg.
68
0
So I have been teaching myself some c++, and as I tried to do an example my guide book showed me: that is where things went awry. The book I have been using is Programming:Principles and Practice Using C++ By Bjarne Stroustrup. I used the solution for the example where I got stuck on a certain part - I did not understand exactly what the book was doing at that part, but threw it in anyway. Even with the solution as a guide, my code still seems to produce errors. Here is the code:

//Write a program that prompts the user to enter two integer values.
//Store these values in int variables named val1 and val2. Write your program to determine the smallest,
//largest, sum, difference, product, and ratio of these values and report them to the user.

#include <iostream>
#include <conio.h>

using namespace std;

int main ()

{
int val1=0;
int val2=0;
cout <<"Please enter two integer values separated by a space: ";
cin >> val1 >> val2;

if (!cin) error ("something went bad with the read");
cout << "values : "<< val1 << "," << val2 << "\n";

if (val1 < val2) << val1 << "is smallest. \n";
if (val2 < val1) << val2 << "is smallest. \n";
if (val1==val2) cout << val1 << "and" << val2 << "is equal.\n";

cout << "sum" << val1+val2 << "\n";
cout << "product : " << val1*val2 << "\n";
if (val2==0)
cout << "good try! but I don't divide by zero!\n";
else
cout << "ratio (val1/val2): " << val1/val2 << "\n";

if (val1<0) cout << val1 << "is negative\n";
cout << "difference (val1-val2) :" << val1-val2 << "\n";
if (val2 !=0) cout << "remainder (val1%val2):" << val1%val2 << "\n";
cout << "square(val1) : " <<val1 * val1 << "\n";

}
catch (runtime_error e) {
cout << e.what () << "\n";

getch ();
return 0;
}

The first error is this if (!cin) error ( "something went bad with the read"); - this is what the solutions say, I haven't seen this before and am not sure how to go about fixing this program.

Thank-You in advance!
 
Technology news on Phys.org
  • #2
Ok, I actually took your code and compiled it, and here is what I got:

error C3861: 'error': identifier not found
error C2143: syntax error : missing ';' before '<<'
warning C4390: ';' : empty controlled statement found; is this the intent?
error C2143: syntax error : missing ';' before '<<'
warning C4390: ';' : empty controlled statement found; is this the intent?
error C2059: syntax error : 'catch'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)

Did you really copy this from the book ?
If so, maybe they made mistakes for you to find them, as an exercise ?
If not, here are the mistakes:

- if (!cin) error ("something went bad with the read");
error doesn't seem to be defined, check in what .h it should be declared

- if (val1 < val2) << val1 << "is smallest. \n";
Something is missing here. Didn't you forget a cout ?

- if (val2 < val1) << val2 << "is smallest. \n";
Same here.

- catch (runtime_error e) {
Where is the try ? And why is the catch outside of the main function ?

All the other errors you get come from these.
 
Last edited:
  • #3
And here is the fixed version of your code.

Code:
#include <iostream>
#include <conio.h>

using namespace std;

void error(const char* str)
{
    cout << str;
    exit(0);
}

int main ()

{ 
    try
    {
    int val1=0;
    int val2=0;

    cout <<"Please enter two integer values separated by a space: ";
    cin >> val1 >> val2;

    if (!cin) error("something went bad with the read");
    cout << "values : "<< val1 << "," << val2 << "\n";

    if (val1 < val2) cout << val1 << "is smallest. \n";
    if (val2 < val1) cout << val2 << "is smallest. \n";
    if (val1==val2) cout << val1 << "and" << val2 << "is equal.\n";

    cout << "sum" << val1+val2 << "\n";
    cout << "product : " << val1*val2 << "\n";
    if (val2==0)
        cout << "good try! but I don't divide by zero!\n";
    else
        cout << "ratio (val1/val2): " << val1/val2 << "\n";

    if (val1<0) cout << val1 << "is negative\n";
    cout << "difference (val1-val2) :" << val1-val2 << "\n";
    if (val2 !=0) cout << "remainder (val1%val2):" << val1%val2 << "\n";
    cout << "square(val1) : " <<val1 * val1 << "\n";

    }

    catch (runtime_error e) {
        cout << e.what () << "\n";

        getch ();
        return 0;
    }

}

Note that I added the error function. I'm not quite sure about what your book meant as I don't remember if 'error' is part of the standard C++ library (I don't use it much ;))
 
  • #4
Ah thank you for your help! This book is quite the nuisance - there is no explicit indication that I supposed to find any errors - for the exercise was merely create so and so of a program - after trying to compile your code these are the current errors:

ex4 cap 3.cpp: In function `int main()':
ex4 cap 3.cpp:44: error: `runtime_error' has not been declared
ex4 cap 3.cpp:44: error: invalid catch parameter
ex4 cap 3.cpp:45: error: `e' undeclared (first use this function)
ex4 cap 3.cpp:45: error: (Each undeclared identifier is reported only once for each function it appears in.)
make.exe: *** ["ex4 cap 3.o"] Error 1
Execution terminated

I also do not know what's the deal with the error function - mind you this ex was in chapter 3 of the book, so I am surprised some higher level coding was introduced since it was not covered in the chapter. I am not sure why there is a main int error - I usually get those if I have more than one file in a same project and explicitly defined int main1 or main2 etc. but this is the only file in this project . The last three errors are things I have no idea of for I was not introduced to them yet. So I wouldn't know how fix them without some prodding in the right direction. Any ideas?
 
  • #5
Ok hmm. It might depend on your compiler. Which one are you using ?

A quick fix would be to replace:

Code:
    catch (runtime_error e) {
        cout << e.what () << "\n";

        getch ();
        return 0;
    }

by

Code:
    catch (exception& e) {
        cout << e.what () << "\n";

        getch ();
        return 0;
    }
 
  • #6
im using bloodshed dev version 4.9.9.2 - and the program did run successfully! thanks for that! - but upon trying to test it: i submitted two integer values and the result window closes immediately after - I am not sure where in my code where that would enable the program to do that - or what errors or flawed syntax in code would do that as well.
 
  • #7
Ok glad to know that it worked.
If the window closes right away, don't worry you've done nothing wrong !
Actually, when you enter the two ints, the program keeps going and then comes to an end.
When it ends, the console window closes (it's the normal behavior).

You have two solutions then. Either configure the console window not to close, or do
a little workaround by adding a getch() at the end of the program. It will then pause
until you press a key before closing.
 
  • #8
I don't know this particular book, but as far as I remember from Stroustrup's "C++ Programming Language" examples he gives are not necessarily correct programs intended to be compiled as shown. They sometimes use some functions that are not defined, but it is clear what they should do - like, say "display_error_message()". I think this is called top-down approach - you first define the general framework, later you write code that executes tasks as assigned. Could be book you have mentioned is different.
 
  • #9
well I agree with you, and a lot of the chapter's and example questions are formatted in that way - except this was in the exercises section and he explicitly states in the solutions:
// note that different compilers/SDEs keep header files in different places
// so that you may have to use "../std_lib_facilities.h" or "../../std_lib_facilities.h"
// the ../ notation means "look one directly/folder up from the current directory/folder"

so now I'm thinking perhaps I should get more libraries, I mean he likes to use std_lib_facilities.h in his solutions - I have been using iostream, but they have always worked before.

*also in this case there was just an example - usually he'll post a code and he would state that there would be errors or we would have enter certain functions - there was no indication for a top down approach in this section of the chapter :/
 

Related to When Even The Posted Solution Isn't Working

1. Why isn't the posted solution working for my specific problem?

There could be several reasons why the posted solution is not working for your specific problem. One possibility is that your problem may have different underlying factors or variables that were not accounted for in the posted solution. It is also possible that there was a mistake in implementing the solution or that the solution itself is not accurate. It is important to carefully analyze the problem and the solution to determine the cause of the issue.

2. What steps can I take to troubleshoot when the posted solution isn't working?

The first step in troubleshooting when the posted solution isn't working is to carefully review the solution and the problem to identify any potential errors or discrepancies. You can also try to break down the solution into smaller steps and test each one individually to pinpoint where the issue may be occurring. It may also be helpful to seek out additional resources or consult with other experts in the field to get a fresh perspective on the problem.

3. Are there any common mistakes that can cause a posted solution to not work?

Yes, there are several common mistakes that can lead to a posted solution not working. These can include errors in the code or calculations, incorrect assumptions about the problem, or overlooking important details. It is always important to double-check your work and thoroughly test the solution before assuming it is correct.

4. What should I do if I am unable to get the posted solution to work?

If you are unable to get the posted solution to work, it may be helpful to take a step back and approach the problem from a different angle. Sometimes, a fresh perspective or a different approach can lead to a breakthrough. You can also try reaching out to the person who posted the solution or other experts in the field for guidance or clarification.

5. How can I avoid encountering issues with posted solutions in the future?

To avoid encountering issues with posted solutions in the future, it is important to thoroughly understand the problem and the solution before implementing it. It can also be helpful to practice problem-solving and critical thinking skills to be able to identify potential errors or gaps in the solution. Additionally, staying up-to-date with advancements and changes in the field can also help in finding accurate and effective solutions.

Similar threads

  • Programming and Computer Science
Replies
4
Views
798
  • Programming and Computer Science
2
Replies
39
Views
3K
Replies
10
Views
972
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
896
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top