Click here to Skip to main content
14,639,947 members
Rate this:
Please Sign up or sign in to vote.
I just started using Visual Studio for the first time. I wanted to try and use my knowledge of C++ to build a simple windows application. The application is supposed to calculate a simple, one expression derivative such as 4x^2, 2x^3, 24x^5 etc. (I was going to make it do more but I had to make sure this worked first) However, it doesn't seem to work (at least not on Visual Studio).

What I have tried:

This is the code for the button that the user will press to calculate. The answer is supposed to be displayed in label4:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

		std::string expression;
		msclr::interop::marshal_context context;
		expression = context.marshal_as<std::string>(textBox1->Text);

		std::string firstNum;
		std::string firstExpr;

		for (int i = 0; i < expression.size(); ++i)
		{
			if (isdigit(expression.at(i)) == true)
			{
				firstNum = firstNum + expression.at(i);
			}
			else
			{
				int num = atoi(firstNum.c_str());
				std::string powNum;
				powNum = powNum + expression.at(i + 2);
				int pow = atoi(powNum.c_str());
				num = num * pow;
				--pow;
				firstExpr = std::to_string(num) + "x^";
				firstExpr = firstExpr + std::to_string(pow);
				break;
			}
		}
		label4->Text = context.marshal_as<String^>(firstExpr);
	
	}

at first I used stoi() instead of atoi() but I kept getting an exception thrown saying stoi(out of range). I Copied and pasted the code to Notepad++ (with very minor tweaks) and compiled it on the GNU compiler through the Putty terminal and it worked just fine. I gave up with using stoi() and tried using atoi() and c_str() to first turn the string into a c string then convert it to an int. However, although I no longer get an exception, the output is always 0x^-1. I tried as much as possible to use the C++ stuff I am familiar with (hence why I did everything using C++ strings instead of Microsoft's String ^) since I am new to this but It's just not working in Visual Studio for some reason. Could I please get some help with this, I would greatly appreciate it. Thank you.
Posted
Updated 24-Apr-20 7:51am
Comments
Rick York 24-Apr-20 13:19pm
   
It is important to note this is managed C++ and not standard C++. I suspect that is a large part of your problem. I know very little about that so I can't help you.
Member 14767000 24-Apr-20 17:07pm
   
Thank you for you're input!
jeron1 24-Apr-20 13:31pm
   
Can you set a breakpoint and step though it?
Member 14767000 24-Apr-20 17:10pm
   
I tried, but I got a correct solution already. It turns out that doing if(isdigit(...) == true) isn't a good idea and that I am supposed actually just do if((isdigit(...)). Thank you for your responding!

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

avoid such code lik
if (isdigit(expression.at(i)) == true)
use more explicit code like
CHAR c = expression.at(i);
if( isdigit(c) ) //no need to check for true
to see better what is going on.

One very important point is that you use the ANSI version, but your string class may work with unicode. Read the details about atoi, _atoi_l, _wtoi, _wtoi_l. My tip is that you better use _ttoi.

Learn to use the debugger, because I am sure that these exceptions are BUGS in cour code.
   
Comments
Member 14767000 24-Apr-20 14:11pm
   
Ok so... doing what you said and changing:

if (isdigit(expression.at(i)) == true)

to:

CHAR c = expression.at(i);
if( isdigit(c) )

seems to have fixed it. It now gives me the right output. Thank you so much, but could you please explain to me why doing the former is not ok, cause I've done that a lot in the past before I started using visual. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100