-3

I am having a problem with an error function which' purpose it is to check a variable and in a certain case tell me the variables name and its value.

I want to give the value as well as the name (as a string) to the function but I am having problems declaring a string somehow (Eclipse MinGW C++).

If anyone could point me to my mistake or show me a workaround that would be great!

This is the code:

#include <string>
#include <iostream>

using namespace std;

int *ierr;

std::string(varname); //problem here, doesnt recognize the string

void error(double varvalue, std::string varname)
 {
        if (varvalue == 0 ) {
            *ierr = 11;
            cout << "Error: " << varname << " has an invalid value (equal 0)";
            cout << "Error number " << *ierr << endl;
            return;
        }
        if (varvalue < 0 ) {
            *ierr = 10;
            cout << "Error: " << varname << " has an invalid value (" << varvalue << " , smaller 0)";
            cout << "Error number: " << *ierr << endl;
            return;
        }
 }

int main() {
    int Par = 0;
    error(Par,"Par"); //test variable
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
AndiJ
  • 3
  • 2
  • Which errors do you get? What purpose is `std::string(varname);` for at all? – πάντα ῥεῖ Sep 19 '18 at 06:18
  • 1
    Where is `varname` defined? – P.W Sep 19 '18 at 06:21
  • The thing is I dont get a compiling error or anything similar. When I try to compile it at the point of output I get a Windows error: "Testing.dll has stopped working" and it closes. – AndiJ Sep 19 '18 at 06:34
  • What is this supposed to do `std::string(varname);`? Why don't you just remove it? – Galik Sep 19 '18 at 06:35
  • std::string(varname); is the definition. I want to declare varname as a string which gives a variable name to the function. – AndiJ Sep 19 '18 at 06:35
  • 1
    A bigger problem is that you're seg faulting all over the place because of `int *ierr` – GKE Sep 19 '18 at 06:36
  • 1
    Yeah, as GKE points out, the pointer ierr is uninitialized and that's what is giving you trouble. You should replace that line with int *ierr = new int(0); – Massimo Di Saggio Sep 19 '18 at 06:43
  • Welcome to Stack Overflow! Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. But I think your question is still not answerable. **You** should [edit] your question now, to add missing details (see [mcve] ). Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Sep 19 '18 at 06:44
  • Ok I changed *ierr to ierr and now the program is actually working. Thanks. Thought the problem was somewhere else – AndiJ Sep 19 '18 at 06:53

1 Answers1

0

You did not correctly define a string value. The syntax is incorrect. On a more serious note, you declare an uninitialized pointer *ierr and it's causing seg faults at run-time.

I am having a problem with an error function which' purpose it is to check a variable and in a certain case tell me the variables name and its value.

The code below will do that for you but it's important to understand when to use pointers.

Furthermore you should probably read up a little on strings and get familiar with what a string definition, copy, initialization, etc, look like.

#include <string>
#include <iostream>

using namespace std;

void error(double varvalue, std::string varname)
 {

        if (varvalue == 0 ) { //if varvalue is equal to 0

            cout << "Error: " << varname << " has an invalid value (equal 0)";
            cout << "Error number " << varvalue << endl;
            return;
        }

        if (varvalue < 0 ) { //if varvalue is less than 0

            cout << "Error: " << varname << " has an invalid value (" << varvalue << " , smaller 0)";
            cout << "Error number: " << varvalue << endl;
            return;
        }

        if (varvalue > 0) { //if varvlue is greater than 0

            cout<<varname<<" has value: "<<varvalue<<endl;

        }
 }

int main() {

    int Par = 10; // Set initial Par value to 10

    error(Par,"Par"); //test variable

}
GKE
  • 960
  • 8
  • 21
  • Thanks, *ierr was my problem. Changed it to ierr and now its working fine. I copied that variable from a older program which I should have checked better. – AndiJ Sep 19 '18 at 06:59