I'm trying to write a program that simulates darts being thrown at a standard curve. Whenever I get close to debugging the entire thing something else pops up. So far I am getting a lot of errors like:
Error: variable is not declared in this scope
Also there's an error I have no idea how to fix which has to do with C++ comparing pointers and integers
I'm pretty new to C++ so any pointers would be greatly appreciated.
Here's what I got so far.
note: errors are on lines 67, 70, 72, and 75.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double seed(int darts, int x);
int main ()
{
int darts, x_max;
double area;
char again = 'y';
char giveDarts;
while (again == 'y' || again == 'Y');
cout << "Run program (y/n)?";
cin >> giveDarts;
switch (giveDarts) {
case 'y':
case 'Y':
cout << "Enter the ammount of darts to be thrown: "; //since we are simulating DARTS I will use the varible darts instead of "NumberOfSamples"
cin >> darts;
srand(darts);
default:
break;
}
cout << "Enter maximum value of x: ";
cin >> x_max;
while (x_max < 0);
cout << "Please enter a positive value of x: ";
cin >> x_max;
cout << endl;
srand(time(NULL));
area = seed(darts, x_max);
cout << "Estimate of area under curve is: " << area << endl;
cout << "Go again? ";
cin >> again;
return 0;
}
double seed(int darts, int x_max)
{
int i, num_darts=0; //num_darts instead of SamplesInsideArea.
double area;
for(i=1; i<=darts; i++) // for loop
{
double x, y;
double pi = 3.14;
double n (double t);
return 1/sqrt(2*pi)*exp(-pow(t,2)/2); //error:'t' was not declared in this scope
x = rand() / static_cast<double>(RAND_MAX);
y = rand() / static_cast<double>(RAND_MAX);
n(0) = (x*x_max + y*y_max); //error: y_max was not declared in this scope
if(num_darts <= n) //error: ISO C++ forbids comparison between pointer and integer
num_darts++;
area*n(0)+0.5 = static_cast<double>(num_darts)/darts; //error: invalid Ivalue in assignment.
}
return area;
}
This line:
double n (double t);
is prototyping a function n that takes one parameter double t. This is causing two of the errors:
error: 't' was not declared in this scope (because function prototypes don't declare variables)
error: ISO C++ forbids comparison between pointer and integer (because n is a pointer to a function)
Did you mean this to be a function prototype? If not, what did you mean?
The error error: y_max was not declared in this scope is straight-forward. y_max isn't declared anywhere.
This line:
area*n(0)+0.5 = static_cast<double>(num_darts)/darts; //error: invalid Ivalue in assignment.
The error error: invalid Ivalue in assignment is because you can't assign a value to an expression. What did you intend to do here?
Additionally, there are some other problems:
This line:
while (again == 'y' || again == 'Y');
will cause your program to go into an infinite loop, since you set again = 'y' just before it, and the semicolon tells the compiler this:
while (again == 'y' || again == 'Y')
{
// do nothing
}
To fix this, remove the semicolon and put braces around the code that needs to be inside the while loop. The same issue exists later too (while (x_max < 0);).
Someone else pointed out this one:
return 1/sqrt(2*pi)*exp(-pow(t,2)/2);
which occurs in the middle of the function. This will cause that function to finish immediately and return the calculated value. Is this what you intended? The code after this line will never run.
More problems:
This code doesn't handle the N/n case. The program will not stop when you type 'n', and will probably crash.
switch (giveDarts) {
case 'y':
case 'Y':
cout << "Enter the ammount of darts to be thrown: "; //since we are simulating DARTS I will use the varible darts instead of "NumberOfSamples"
cin >> darts;
srand(darts);
default:
break;
}
cout << "Enter maximum value of x: ";
Use braces to control loops, not whitespace. Instead of this:
while (x_max < 0);
cout << "Please enter a positive value of x: ";
cin >> x_max;
cout << endl;
You want this:
while (x_max < 0)
{
cout << "Please enter a positive value of x: ";
cin >> x_max;
cout << endl;
}
This line:
area*n(0)+0.5 = static_cast<double>(num_darts)/darts;
If you're trying to set area, this needs to be in the form:
area = static_cast<double>(num_darts)/darts; // n(0)+0.5 goes where??
When you are first learning to program C++, I suggest that you declare and define all of your functions at the global level. This means that lines like double n (double t); should never appear inside any braces. So to fix part of the problem with your code, move these two lines of code:
double n (double t);
return 1/sqrt(2*pi)*exp(-pow(t,2)/2);
outside of the seed() function and make a few minor modifications so it looks like this:
double n (double t) {
return 1/sqrt(2*pi)*exp(-pow(t,2)/2)
}
This should help you in the right direction. (Just be sure that pi is declared either as a global constant.)
Related
I want to create a program that when a user inputs something that I didn't define, the program prompts him again.
I did it with if statements but it only loops for 1 time and doesn't do it again. I tried loops but whenever the input is false it just breaks the condition and refuses all inputs alike. In c++.
Any help is much appreciated.
#include <iostream>
#include <string>
using namespace std;
void xD(){string x;
do{cout << "Retry\n";
cin >> x;}while(true);}
//declaring a function to make the shop
void shop(){
string x;
float coins = 500;
float bow_cost = 200;
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
cin >> x;
// if u chose bow you get this and get to choose again
if (x == "bow"){
cout << "you bought the bow.\n you now have " <<coins - bow_cost << " coins." << endl; cin >> x;}
/*now the problem that whenever I excute the code and type something other than bow it gives me the cin only once more and then fails even if I type bow in the 2nd attempt*/
//in my desperate 5k attempt, I tried creating a function for it.. no use.
//i want it o keep prompting me for input till i type "bow" and the other block excutes. but it never happens.
else{xD();}
}
int main(){
string name;
string i;
cout << "if you wish to visit the shop type \"shop\"\n";
cin >> i;
if(i == "shop"){shop();}
else{cin >> i;}
return 0;
}
The problem lies on the condition in this loop block
void xD(){
string x;
do{
cout << "Retry\n";
cin >> x;
}while(true);
}
The while(true) condition makes it loops forever regardless of the input. To fix this, you can change the condition:
void xD(){
string x;
do{
cout << "Retry\n";
cin >> x;
}while(x!="bow");
cout << "you bought the bow. and some other messages"<<endl;
}
That should work. However, it is still too complicated for me. This can be simplified into the snippet below:
void shop(){
string x;
float coins = 500;
float bow_cost = 200;
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
cin >> x;
while (x!="bow"){
cout << "Retry\n";
cin>>x;
}
cout << "you bought the bow.\n you now have " <<coins - bow_cost << " coins." << endl; cin >> x;
}
Instead of doing this approach (which is checking the condition only once):
if (x == "bow"){
cout << "you bought the bow.\n you now have " <<coins - bow_cost << "
coins." << endl; cin >> x;
} else{
xD();
}
which is actually a RECURSIVE invocation to the method xD()
you should do a do-while loop,
example:
while (x.compare("bow") != 0)
{
cout << "sorry, wrong input, try again...";
cin >> x;
}
note the use of the compare method instead of the == operator
here more about it in the documentation
You can use return value of cin >> [your input object] here to check status or istream's method fail(). As soon as input stream fails to parse whole or part of streams it fails and stay in state of failure until you clear it. Unparsed input is preserved (so you can try to parse it differently?)m so if you try to >> again to object of same type, you'll get same failure. To ignore N chars of imput, there is method
istream::ignore(streamsize amount, int delim = EOF)
Example:
int getInt()
{
while (1) // Loop until user enters a valid input
{
std::cout << "Enter an int value: ";
long long x; // if we'll use char, cin would assume it is character
// other integral types are fine
std::cin >> x;
// if (! (std::cin >> x))
if (std::cin.fail()) // has a previous extraction failed?
{
// yep, so let's handle the failure, or next >> will try parse same input
std::cout << "Invalid input from user.\n";
std::cin.clear(); // put us back in 'normal' operation mode
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // and remove the bad input
}
// Thechnically you may do only the above part, but then you can't distingusih invalid format from out of range
else if(( x > std::numeric_limits<int>::max()) ||
( x < std::numeric_limits<int>::min()))
{
std::cout << "Invalid value.\n";
}
else // nope, so return our good x
return x;
}
}
For strings parsing is almost always successful but you'll need some mechanism of comparison of string you have and one that is allowed. Try look for use of std::find() and some container that would contain allowed options, e.g. in form of pair<int,string>, and use int index in switch() statement (or use find_if and switch() within the function you give to it).
Consider that if() statement is a one_direction road, it checks the condition and if the condition was satisfied it goes to its bracket and do blah blah blah , if there is any problem with condition compiler passes ifand jump to compile other codes.
Every time that you begin to compile the codes it begins from int main() function. You did the wrong thing in the if and else statements again
Here is the correct code .I did the necessary changes.
#include "stdafx.h"
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
#define coins 500 ;
#define bow_cost 200 ;
int shop(string x)
{
//There is no need to allocate extra memory for 500 and 200 while they are constant.``
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
do
{
cout << "Input another :\n";
cin >> x;
if (x == "bow")
{
return (coins - bow_cost); //return to function as integer
}
} while (true);
}
int main()
{
string name, i;
cout << "if you wish to visit the shop type \"shop\"\n";
cin >> i;
if (i == "shop")
{
cout << "Input :\n";
cin >> name;
cout << shop(name) << "you bought the bow.\n you now have " << " coins." << "\n";
}
//argument passed to shop funnction parameters.
system("pause");
return 0;
}
I'm currently learning about functions in C++ and am currently working on a homework assignment with functions being the main thing.
Currently, I'm trying to make a grade calculator with every operation of the process being split into a function of its own.
Here's the code:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
void getHWGrades(int homeworks[], int size)
{
cout << "\nEnter the grades, out of 100 points, for the 9 homeworks you completed." << endl;
cout << "Note that Homework 10 is given to you for free, but is the same grade \nas homework 9.\n" << endl;
for (int i = 0; i < 9; i++)
{
cout << "Homework " << i + 1 << ": ";
cin >> homeworks[i];
while (homeworks[i] > 100 || homeworks[i] < 0)
{
cout << "Invalid grade, input homework grade again: ";
cin >> homeworks[i];
}
}
homeworks[9] = homeworks[8];
cout << "Homework 10: " << homeworks[9];
}
double quizAverage()
{
double quizPts;
cout << "Input your in class quiz average: ";
cin >> quizPts;
return quizPts;
}
double labAverage()
{
double labPts;
cout << "Input your lab average: ";
cin >> labPts;
return labPts;
}
double teamProject()
{
double teamPts;
cout << "Input your team project grade: ";
cin >> teamPts;
return teamPts;
}
int exam1()
{
int exam1Pts;
cout << "Input your exam1 grade: ";
cin >> exam1Pts;
return exam1Pts;
}
int exam2()
{
int exam2Pts;
cout << "Input your exam2 grade: ";
cin >> exam2Pts;
return exam2Pts;
}
double hwAverage(int homeworks[], int size)
{
double total = 0;
double homeworkAverage = 0;
for (int i = 0; i < size; i++)
{
total = total + homeworks[i];
}
homeworkAverage = (total*1.0) / 10;
return homeworkAverage;
}
double currentPoints(double& quizPts, double& labPts, double& teamPts, double& homeworkAverage, int& exam1Pts, int& exam2Pts)
{
double totalPts = ((quizPts / 100.0) * 10) + ((labPts / 100.0) * 10) + ((teamPts / 100.0) * 15) + ((homeworkAverage / 100.0) * 20) + ((exam1Pts / 100.0) * 10) + ((exam2Pts / 100.0) * 15);
cout << "\nYour current points (out of the 80 total available), stand at: " << totalPts;
return totalPts;
}
double currentAverage(double& totalPts)
{
double availableAverage = totalPts*(100.0 / 80);
cout << "\nYour current average is: " << availableAverage;
return availableAverage;
}
int main()
{
// keep the console from closing in visual studio
char charer;
double totalPts;
double quizPts, labPts, teamPts, homeworkAverage;
int exam1Pts, exam2Pts;
const int ARRAY_SIZE = 10;
int hwArray[ARRAY_SIZE];
getHWGrades(hwArray, ARRAY_SIZE);
quizAverage();
labAverage();
teamProject();
exam1();
exam2();
currentPoints(quizPts, labPts, teamPts, homeworkAverage, exam1Pts, exam2Pts);
currentAverage(totalPts);
cin >> charer;
}
My issue, which I believe lies in the functions currentPoints and currentAverage, is that when I run this totalPts outputs as -5.09078e+61 and as a follow up result with the currentAverage function, availableAverage outputs as -1.157e+62.
I'm sure that the issue has to do with how I'm passing the values from function to function (which I doubt I'm doing correctly).
How would I go about fixing this issue?
Thank you in advance.
You need to store the return value from currentPoints() function, like this.
totalPts = currentPoints(quizPts, labPts, teamPts, homeworkAverage, exam1Pts, exam2Pts);
currentAverage(totalPts);
Reason is, you declared "totalPts" as local variable in currentPoints().
"Local variables has function scope only, it is undefined to main function".
Do this for all other
functions(quizAverage,labAverage,teamProject,exam1,exam2, hwAverage,currentAverage)
I hope, this will solve the issue !!!
The problem is not about functions, it's about variables.
Let's take quizPts for instance:
In the main method, you declare this variable, but then you don't do anything with it before sending it to currentPoints. Therefore it has an undefined value when you do so (undefined often looks like random in C).
The other variable quizPts you use in quizAverage have the same name but is not the same for the compiler.
Try in your main:
quizPts = quizAverage();
You asked
How would I go about fixing this issue?
And the answer is "Use the debugging tool with "watches" window open in your favorite IDE".
It's always very difficult to find an error simply by re-reading the code, but in the debugger you can see all the values of your variables at each moment of time. Specifically, in this example, you would realize that your variables have garbage values from the very beginning (are not initialized), and this value never changes.
Using this approach you could find the reason yourself in time less than necessary to write this SO question. I hope this will help you to save your time in future.
The problem is you use the variables such as quizPts and labPts without storing any value in them. In your case, you have to store the return value of the function to the corresponding variable before using it. For example, do the same as the following statement:
quizPts = quizAverage();
My formatting is really bad, and I'm sorry for that, but I'm still in the very early stages of learning C++. Anyway, no matter what I try, I always get an expected unqualified ID error on line 51. Any assistance would be greatly appreciated! And no, the code is not finished, its due March 20th, just wanted to start on it early. And this is NOT how I wanted to format it, but this is how it was stated on the rubric. I'm brand new to functions, so yeah, any help would be awesome!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int menu (int); //Menu prototype for 4 options
int main()
{
const int numWidth = 4;
int choice, pick;
cout << "Welcome! Please select your choice by entering 1, 2, 3, or 4!" << endl;
cout << "1:";
cout << right << setw(numWidth) << " Enter the Grades" << endl;
cout << "2:";
cout << right << setw(numWidth) << " Display the Grades" << endl;
cout << "3:";
cout << right << setw(numWidth) << " Show overall Grade" << endl;
cout << "4:";
cout << right << setw(numWidth) << " Exit the program" << endl;
cout << "Select your choice and press enter ";
cin >> choice;
pick = menu(choice);
cout << pick;
if (choice ==1)
{
cout << ". Please enter your grades.\n";
}
float AssignmentGrade(float, float, float, float); // Assignment Grade Prototype
{
float asgnment1, asgnment2, asgnment3, asgnment4, total;
cout << "First, enter your 4 assignment grades ";
cin >> asgnment1 >> asgnment2 >> asgnment3 >> asgnment4;
total = AssignmentGrade (asgnment1, asgnment2, asgnment3, asgnment4);
cout << total;
return 0;
}
}
float AssignmentGrade (float num1, float num2, float num3, float num4)
{
float asgnmentgrade;
asgnmentgrade = num1*0.05 + num2*0.05 + num3*0.05 + num4*0.05;
cout << "Total points, including weights, for assignment grade is ";
return asgnmentgrade;
}
float LabTestGrade (float, float, float); // Right here, line 51, is where I get expected unqualified ID error before {
{
float lab1, lab2, lab3, total;
cout <<"Next, please enter your 3 lab test scores!";
cin >> lab1 >> lab2 >> lab3;
total = LabTestGrade (lab1, lab2, lab3);
cout << total;
return 0;
}
float LabTestGrade (float lab1, float lab2, float lab3)
{
float LabGrade;
LabGrade = lab1*0.10 + lab2*0.10 + lab3*0.10;
cout << "Total points earned from lab tests is";
return LabGrade;
}
int menu (int num)
{
int option;
option = num;
cout <<"You have selected ";
return option;
}
Delete unnecessary ; in your 51st line: float LabTestGrade (float, float, float);
Dont forget that ; is used for the end of a command. If you define a function, you don't use it. Look at th following model:
float function (float parameter) {
command();
command();
return 0;
}
Edit: There's the same syntax error in your 31th line.
Delete ; again in float AssignmentGrade(float, float, float, float);
You will find yourself running into a few other problems with this function, apart from the original unqualified ID that #Nichar answered.
At Line 50 you have:
float LabTestGrade (float, float, float) { // Function Code }
and at Line 60 you have:
float LabTestGrade (float lab1, float lab2, float lab3) { // Function Code }
This will cause a compile error as you are trying to define another function with the exact same name and parameters. (Redefinition error)
Although these functions may look slightly different as you have named the parameters of the second one (float lab1, float lab2, float lab3), to the compiler it will simply see two functions that are named the same and take in the same types as parameters.
That being said, you can delete the parameters in the first LabTestGrade() as they aren't actually being used inside its function.
float LabTestGrade() { // Function Code }
Finally, you are also going to want to swap around the order that these two functions are being defined. I suggest googling C++ Scope but basically the first LabTestGrade() is making a call to the second LabTestGrade(float lab1, lab2, lab3) like so.
total = LabTestGrade(lab1,lab2,lab3);
However, the second LabTestGrade hasn't been defined yet within your program so it will try to call the first LabTestGrade() again and give you an error because the first LabTestGrade() doesn't take in any parameters now and this line of code is trying to pass into it three floats into a function that takes in nothing.
Alternatively, you could just declare the functions above your main function like you had done for your menu function.
float LabTestGrade();
float LabTestGrade(float lab1, float lab2, float lab3);
int menu(int) // Menu prototype for 4 options
Hi even im a beginner but heres what i think -
First of all
The menu prototype in the beginning is incorrect . Prototypes should be exactly same as the function itself .
Correct prototype will be
int menu (int num);
In line 51 , in the parameter bracket, try writing the full name of the float types you r trying to get. Also remove the semicolon. Ex:-
float LabTestGrade (float a , float b , float c) { }
A similar mistake is made in the AssignmentGrade function which takes 4 floats.
Try writing the full name as parameters. Remove the semi colon.
Write the function prototypes separately in the beginning outside the main.
You have to create a separate prototype for every function you make and remember - prototypes should have the same name as the function. Just you need to replace the curly braces with a semi colon.
Hope this helps.
EDIT:- You can also try writing the functions outside main and then use prototypes in the beginning outside the main.
I'm an ICT student, studying C++, and I find it very interesting. However, while experimenting, I came up to something I can't fix myself nor find on the net.
This is the code:
#include <iostream>
using namespace std;
float average(float array[10], float &average){
int i = 0;
for( i = 0; i != 10; i++ ){
cout << "Vett ("<<i<<") = ";
cin >> array[i];
while(cin.fail())
{
cin.clear();
cin.ignore();
system("Color 5C");
cout << "\nPlease insert a number: ";
cin >> array[i];
}
average = average + array[i];
}
average = average / 10;
return array[10];
return average;
}
main(void){
float vett[10], media;
int i;
char loop;
vett[10] = 0;
media = 0;
do{
system("cls");
system("Color 00");
cout<<"****************************************"<<endl;
cout<<"*** INSER THE DATA TO COMPUTE ***"<<endl;
cout<<"****************************************\n"<<endl;
/* for( i = 0; i != 10; i++ ){
cout << "Vett ("<<i<<") = ";
cin >> vett[i];
while(cin.fail())
{
cin.clear();
cin.ignore();
system("Color 5C");
cout << "\nPlease insert a number: ";
cin >> vett[i];
}
media = media + vett[i];
}
media = media / 10;
*/
average(vett[10],media);
for( i = 0; i != 10; i++ ){
cout << vett[i]<<" ";
}
if(media == 0){
cout << "\nATTENTION the average equals to = "<<media<<endl;
}
else{
cout << "\nThe average is"<<media<<endl;
}
printf("\n");
cout << "\nDo You want to continue? Y/N";
cin >> loop;
}
while(loop == 'Y' || loop == 'y');
system("pause");
}
For some reason I couldn't set in the 'average' function the array as a pointer (&array), perhaps because the array is already a pointer. Nonetheless, removing it gives me the following error:
"Cannot convert 'float' to 'float*' for argument '1' to 'float average(float*,float&)'
If I call the function this way
average(&vett[10],media);
it works, but returns weird values in the array.
As you can see, I commented the same thing I put in the function, which works perfectly, unless..I put it in a function. I assume I've done something wrong with the function call, can anybody help me understand?
First of all, note that main(void){ is not a valid signature for main.
It should be:
int main(void){
Then:
float vett[10];
vett[10] = 0;
This is not valid. Array indices start at 0, so index 10 is out of bounds, as it would require an array with size 11.
Also, as your average function takes as first argument a float array, you'll need to pass it this way:
average(vett,media);
Using:
average(&vett[10],media);
Will pass a pointer to the data located right after the array, so obviously you'll get junk values.
Don't use func(void) in C++, it's some sort of deprecated. Use func() instead.
If you have float vett[10];, then vett[10] is invalid. You must use only vett[0 .. 9]
float average(float array[10], float &average) actually means float average(float *array, float &average). (Second form is more common way to declare pointer arguments.) If you want to call it with vett as argument, just use average(vett, media);Names of an arrays, when used as pointers, are automatically converted to pointer to first element of an array. So hereaverage(vett, media);vett means &vett[0].
The number in the square brackets has two meanings:
In a declaration like float vett[10]; it is the size of the array
When not in a declaration, like average(&vett[10],media); it means the eleventh element of the array.
average(&vett[10],media); is passing the address of the eleventh element to the function. The function interprets it as the beginning of the array, which is wrong and causes undefined behaviour when the elements outside of the array are accessed.
Because you want to pass the whole array, you should use
average(vett,media);
I'll add a little more to the help...
Your average method has two return statements at the end of it. The final one (the one you will be wanting) will never be reached, because the method will return on the first one...
In the function instead of this
float average(float array[10], float &average){
you have to put this :
float average(float *array, float &average){
I am trying to write a code that finds perfect numbers lower than the user's input.
Sample of correct output:
Enter a positive integer: 100
6 is a perfect number
28 is a perfect number
There are no more perfect numbers less than or equal to 100
But when I run my code, I get the error Floating point exception
and can not figure out why. What am I doing wrong?
Here is my code:
#include <iostream>
using namespace std;
bool isAFactor(int, int);
int main(){
int x, y;
int countOut, countIn;
int userIn;
int perfect = 0;
cout << "Enter a positive integer: ";
cin >> userIn;
for(countOut = 0; countOut < userIn; countOut++){
for(countIn = 1; countIn <= countOut; countIn++){
if(isAFactor(countOut, countIn) == true){
countOut = countOut + perfect;
}
}
if(perfect == countOut){
cout << perfect << " is a perfect number" << endl;
}
perfect++;
}
cout << "There are no more perfect numbers less than or equal to " << userIn << endl;
return 0;
}
bool isAFactor(int inner, int outer){
if(outer % inner == 0){
return true;
}
else{
return false;
}
}
The arguments are just swapped. You are calling the function as isAFactor(countOut, countIn) when you should be calling with isAFactor(countIn, countOut)
To clarify #Aki Suihkonen's comment, when performing:
outer % inner
If inner is zero, you will get a divide by zero error.
This can be traced backward by calling isAFactor(0, 1).
It is in your for loop in main.
The first parameter to isAFactor(countOut, countIn) is assigned in the outermost for loop:
for (countOut = 0; ...
Notice the value you are initializing countOut with.
Edit 1:
Change your `isAFactor` function to:
if (inner == 0)
{
cerr << "Divide by zero.\n";
cerr.flush();
return 0;
}
if (outer % inner ...
Place a breakpoint at either cerr line above.
When the execution stops there, look at the Stack Trace. A good debugger will also allow you to examine the parameter / values at each point in the trace.