Visual C++ Problem

Pages: 12
I dropped and dragged a couple buttons and text boxes into visual C++.

Immediately I see the code is mumbo jumbo. So abstracted im screwed. Nevertheless I try to proceed.

What I want to do is to to type into a text box a value and convert it into another value displayed in another textbox.

Now the following code is auto generated by Visual C++ I only edited the last part and its that part that the compiler is throwing an error. So just look at the last two statements in that code right at the end.

Now all values typed into text boxes are essentially strings right? So I use the !isalpha statement to check if what typed in is a numeric character. If it is, I use the atoi function to convert this numeric string into an integer value.
I compile and run and doesnt do it.

Visual C++ does not say why
Visual C++ does not say what line is the problem.
I cannot see how one would use visual c++ to build projects? Is Visual C++ really a compiler or not?

So what is the problem with my code anyway? I think I know. Maybe textBox1->Text expects a character whereas isalpha function expects and int as parameter. How do I fix this if it is indeed the error?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#pragma once

namespace f {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label1;
	protected: 
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Label^  label4;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

	private:
		int feet;
		int inches;
		int result;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(9, 34);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(28, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Feet";
			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(100, 34);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(39, 13);
			this->label2->TabIndex = 1;
			this->label2->Text = L"Inches";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(208, 34);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(39, 13);
			this->label3->TabIndex = 2;
			this->label3->Text = L"Inches";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(12, 72);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(61, 20);
			this->textBox1->TabIndex = 3;
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(103, 72);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(71, 20);
			this->textBox2->TabIndex = 4;
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(211, 72);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(58, 20);
			this->textBox3->TabIndex = 5;
			this->textBox3->TextChanged += gcnew System::EventHandler(this, &Form1::textBox3_TextChanged);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(44, 175);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(62, 44);
			this->button1->TabIndex = 6;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(172, 175);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(62, 44);
			this->button2->TabIndex = 7;
			this->button2->Text = L"button2";
			this->button2->UseVisualStyleBackColor = true;
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(180, 75);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(13, 13);
			this->label4->TabIndex = 8;
			this->label4->Text = L"=";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 266);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void textBox3_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 
			 if (!isalpha(textBox1->Text));
				feet=atoi(textBox1->Text);
			 
				 
		 }
};
}
Last edited on
Ignore that post. I realized it does give you errors and lines at the bottom

It mentions:
error C2664: 'atoi' : cannot convert parameter 1 from 'System::String ^' to 'const char *'


I know atoi takes a char pointer but I thought textBox->Text was a character pointer i.e. a string???!! So How do I get around this?
You shoud use:

int i = int::Parse(str); // string to int

System::String^ str = i.ToString(); // int to string
what sort of language is that???!!! And why doesnt C work? Dont they call it visual C++?
The product is officially described as able to use the C language. Why cant a simple strinf to int conversion not work?
Last edited on
Because that's NOT C. It's a C++ extension called C++/CLI which is there to access .NET from native C++ code. You can write C/C++ code with Visual C++, but the GUI editor creates C++/CLI code. Also, a char* is not the only way to represent a string, a string could also be a wchar_t*, char16_t*, char32_t* or an std::string - and that's not an inclusive list. atoi only works with so called C-strings (which means, char*) - you are aware that you can define own datatypes yes? In this case, it's a System::String^ - which means a managed string.
Oh jesus!

Whats the point then?! I cant go on learning new languages every day for f sake. All these extensions and higher level constructs and functions. Im dissapointed

You can write C/C++ code with Visual C++, but the GUI editor creates C++/CLI code


How do I use pure c/c++ code?
Uh... file->new->project->empty project.
file->new->file->visual c++->C++ file (.cpp)
profit.

As I already said though, that means you can't use the wysiwyg GUI editor - unless you have a version of visual studio that has one for windows resource file. I know mine hasn't.

And why are you "disappointed"? Accessing .NET components is something you'll want to do pretty often when programming for windows, so it's actually pretty helpful. And it's actually not that much you'd have to learn extra. But if you want pure C/C++ - well, just don't use all the managed stuff then.
How do I use pure c/c++ code?


You should look into MFC or otherwise use pure Windows API to create and manage windows/controls. The usual recommendation is the book Programming Windows by Charles Petzold. It's old but most of it still applies, and it makes a great beginner guide and reference.
@OP: If you don't notice the additional keywords and the "strange" syntax, I'm not 100% sure your C++ is up to par with Windows Programming. My advise before you go into this any further: Determine your focus. If you are programming for Windows only, you want to do nice GUI's and not code too much, C++/CLI and the .Net framework is for you; hell maybe even C# which is easier. If you want to learn deeper, more complex stuff, align controls in runtime by calculating coords, and doing all sorts of handle management, then go with pure C++, but learn it well first. Only after you have learned C++ the language, go into learning Windows programming.
Hi webJose,

Thanks for your input, I definitely agree. I need to ask you for more of your personal advice.

Let me explain my situation a little bit better.

I am learning C programming at the moment. I would describe myself as being pretty clued up on it, and I can code "purely in it". You name it linked lists, recursive descent parsers etc... I have zero experience in C++. Thats why I was hoping that I could somehow go into Visual C++ without knowing too much C++. Needless to say, I was wrong...

Im very dissapointed to be honest with you. I was thinking to myself "aha, now heres a GUI IDE that I could do using C". Alas, knowledge is based on higher level C++ and CLI.

So what do I do? What GUI can I do? wxWidgets? I dont like the look of it. I love microsoft graphics (API?). Also I don't mind that its not portable outside windows. And despite it being in C++, I still find aspects of it very user friendly.

I don't know what to do. I'm frustrated at being held by my ankles tied down into using this stupid black and white console window. I need to go into GUI's now. This is not to say im expert at pure console programming, I want to learn GUI in parallel. But all options seem hard. I dont think im left with a choice but to learn a somewhat "introductory/basic C++ and CLI" and try get my head around MSVC++. As much as I hate this option, I dont know what choice I have left.
What you suggest?
With C you are pretty much restricted to the Win32 API (http://en.wikipedia.org/wiki/Windows_API ) as far as microsoft GUI programming goes - there are various other GUI libs for C. This is still supported by Microsoft, it's just that Visual C++ express doesn't ship with a drag&drop GUI editor. You would have to use an external resource editor like ResEdit(http://www.resedit.net/ ) for that.

Please note that while GUI programming isn't that terribly difficult, the initialization code - especially in the Win32 API - can be quite daunting.

See here for a win32 tutorial:
http://msdn.microsoft.com/en-en/library/bb384843.aspx
As hanst99 stated, C + WinAPI is kind of daunting (at first, then tedious). C++ classes help you wrap around this tedious, and most of the time boilerplate code so you can focus in the more entertaining part of GUI programming. But like I said: If you don't know C++ the language, it will be hard for you to set up wrappers and in general understand other frameworks like Microsoft's MFC.

Sooo, my advice: Forget C++ for now and jump into C# and .Net. Once you start doing some more advanced .Net stuff, you'll have to get into the realm of P/Invoke and the Windows API for certain bits that are not covered by .Net.

If you really want to keep in the C++ realm, then there is no other decent choice: Learn C++ FIRST.
Sputnik, you seem to me to be a prime canidate for a Win Api coder. What it appears to me you've done wrong is let yourself get 'duped' into Microsoft wizard code. If you enjoy C there is no reason you can't create really decent Win32 GUIs with just C, the Win Api, and MSVC++ Express.


What I want to do is to to type into a text box a value and convert it into another value displayed in another textbox.


How do you want to convert it? If you'll elaborate just a bit, I'll write it up for you in C GUI and show what the code looks like and you can decide if its something you want to pursue. I'm asking because I don't know if you are still following your thread here and I don't want to waste a half hour doing it if you aren't interested.
Thanks all for the responses.

Im still new to most of these terms and concepts so might have couple questions.

I thought visual C++ was the win api just with the "drag and drop" drawing board??

Freddie. I mean, if you type in a text box, you are essentially passing a string correct? All input and outputs are strings or character arrays. So if I put a number in a textbox, I need to convert it to an integer using the atoi function. Only then I can operate with it (say, multiply by 2) and output on the window.

Can you tell me more about winapi. Wiki and google doesnt suffice. I briefly saw Pretzolds (sp?) couple pages and it looks tough. why cant they have a drag and drop feature like visual CPP just in C? Now I must manually code in winapi?
I actually pretty much answered all those above.
"Visual C++" means
1) The VC++ compiler, linker etc
2) All of that integrated into the Visual Studio IDE.

"atoi" is a function that is suitable for only one kind of strings, namely C strings, which are basically just arrays of characters. There are numerous other ways to represent a string.

I already linked you to a Win Api tutorial. You may want to buy a book on the topic - and yes it's tough at first. But I already said so earlier. If you want to have an easy to use GUI API you will probably not get around learning higher level languages than C.

You do have a drag and drop feature in Visual C++, it's just not for the Win32 API, because it's seldom used anymore - Windows GUI programming is nowadays mostly on .NET basis.

PS: wxWidgets does look EXACTLY like what you would get if you used the Win32 API, because it uses the native GUI API's of the environment it is executed in. Which means, if you run a wxWidgets application in windows, you get a windows look-and-feel, on Mac OS it would look like a typical Mac OS application etc.

And I already told you that there are resource editors for Win32 resource files (like ResEdit) out there. But of course you have to write code manually to a degree (though I suppose you could just use a template to get stuff running).
Last edited on
Looked through Pretzolds edition of WinAPI, definetly not for me. I will not go through 1200 pages to get GUI when you can drag and drop. One less option to look at. wxwidgets has no formal documentation. Where can I find a complete reference and text on wxwidgets?
No documentation? I must be seeing things then.

http://www.wxwidgets.org/docs/

The 1200 pages aren't necessary if all you want to do is create GUI's. WinAPI does provide much more functionality than just GUI (and so do most other API's that are primarily used for GUI's - you will hardly find one that only provides functionality for GUI creation.)

As to drag&drop - I believe I already said so too, but there are Drag&Drop editors for pretty much all popular GUI frameworks- that includes, but is not limited to, wxWidgets, Qt, WinAPI, Windows Forms. And while a sophisticated GUI editor is a lot of work, a primitive one can be created quite easily, so even if there isn't one available for whatever you want to use you can just write one.
Last edited on
hanst, im confused as to what wxwidgets actually *is*

Yes I know there are numerous definitions on google

but I dont know what the difference between that and say GTK+ is. Or codeblocks

In my codeblocks I can amazingly open a wxwidgets screen! So Codeblocks is the IDE
GTK+ is the graphics library? wxwidgets is also a graphics library?
My point is GTK+ doesnt have the drag and drop property of wxwidgets. I know I know... you tried explaing it to me earlier but I really need a laymen explanation on this...
I would prefer GTK+ because its in C. Wxwisgets is in C++. But once again I cant do the drag and drop thingy in GTK+ which is the downside. Like I said, I know its been explained to me here, but can it be explained more in laymens terms
An API is basically the thingy that tells you what functions, datatypes and constants a library provides.

A drag and drop editor is basically a program that provides a graphical interface for creating graphical interfaces. It is not a property of the API itself at all.

A program for creating GTK+ GUI's would be, for example, Glade:

http://glade.gnome.org/

I really think you should look up the definitions of the vocabulary you are using, because it seems to me you have right now no real grasp of what's actually going on.

(Alternatively, follow some random tutorial on some GUI API of your choice, and then you will hopefully understand how GUI's work afterwards).
Last edited on
Pages: 12