Click to See Complete Forum and Search --> : Visual C++


Vegas/Newbie
02-27-2003, 01:26 AM
I am a new programmer doing a class project and can't seem to get the help I need. I am running MS Visual C++ Introductory V6.0 on a Win 98 SE system. I cannot get this code to compile without errors. Please help!!!


#include <iostream.h>
#include <string.h>
#pragma hdrstop

int main()
{
string name, num; // this is the line I am having probs with. Is syntax/usage correct?
float g1, g2, g3, avg, ptsneeded;
cout << "Enter Student's Name: ";
cin >> name;
cout << "Enter Student's Number: ";
cin >> num;
cout << "Enter Grade 1: ";
cin >> g1;
cout << "Enter Grade 2: ";
cin >> g2;
cout << "Enter Grade 3: ";
cin >> g3;
avg = (g1+g2+g3)/3;
if (avg >= 65)
{
cout << name << " received a grade of S" << endl;
}
else
{
ptsneeded = 65 - avg;
cout << name << " received a grade of U" << endl;
cout << name << " would have needed " << ptsneeded;
cout << " more points to receive an S" << endl;
}
cin.get();
cin.get();
return 0;
}
:confused:

GaryJones32
02-27-2003, 02:50 AM
hi,
the std lib string.h is those crazy c style char array things.
you have to use or write another class to create a data type String

so you have to declare "string like stuff"

like

char name[10],num[10];

truls
02-27-2003, 04:30 AM
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::cin;
using std::endl;

Ta ta.

Vegas/Newbie
02-27-2003, 10:00 AM
I appreciate the help. The char[10] works well. I know it can also be done with ostring, if I am not mistaken, but you need a directory of files. Is string (yadayada); similar to ostring? Thanks

truls
02-27-2003, 10:28 AM
Replace your header with mine and it'll work.
You can write code like:
string str;
cin >> str;

Vegas/Newbie
02-27-2003, 11:21 AM
truls,
could you be a little more specific with your reply please. Say I wanted a string to represent office, would I write:

code:
------------------------------------------------------------------------------------
#include <string>
string office str;
cin >> office str;
------------------------------------------------------------------------------------

My class is online and my instructor is no help whatsoever. I've gotten more help through this forum. Thanks in advance.

Energon
02-27-2003, 01:24 PM
Don't use cin >> office, use std::getline(cin, office). Otherwise you'll get problems if they put spaces between things.

bwkaz
02-27-2003, 03:02 PM
Originally posted by Vegas/Newbie
Say I wanted a string to represent office Then you would have an #include <string> somewhere, and a using std::string; somewhere else.

Then you'd do this:

string office;

cin.getline(office, 100);
// or
std::getline(cin, office, 100); or something like that.

Vegas/Newbie
02-27-2003, 06:32 PM
Thanks a bunch. Question:
Is there a big difference between Borland C++ and MS Visual C++?

RedMirrorBall
02-28-2003, 02:46 AM
With M$ Visual C++ you get to sell your sell to the devil. I don't think Borland has that option.

Just Joking Kids, I have never used Visual C++

-RMB

bwkaz
02-28-2003, 08:57 AM
There isn't much of a difference in the language (they're both C++ compilers, after all).

But I'm not sure whether Borland gives you access to MFC, and I don't think it does. I think it uses Borland's VCL class hierarchy. I know that Borland C++Builder uses VCL.

hotleadpdx
02-28-2003, 12:03 PM
But I'm not sure whether Borland gives you access to MFC, and I don't think it does. I think it uses Borland's VCL class hierarchy. I know that Borland C++Builder uses VCL.

Since I'm learning C++ (or attempting to in my spare time), what do the two TLA's you used stand for? (MFC, VCL)

spacedog
02-28-2003, 01:11 PM
sorry im pretty late, but in VC++ when using something from the STL just add this line after the includes

using namespace std;

so it would be

#include <iostream>
#include <string>

using namespace std;

//code

Vegas/Newbie
03-01-2003, 03:15 PM
A friend of mine was compiling on his Linux box, helping me out with some of this compiling. He used the following as header files:
---------
#include <iostream>
#include <string>
using std::string;
--------------

They compiled great on his box, but failed miserably on my MS Visual C++. I really hate Microsoft. I am starting to believe that a compiler is a compiler is a compiler as long as Microcrap doesn't create it:-).
BTW. Thanks to all that have responded

bwkaz
03-03-2003, 02:24 PM
MFC are the Microsoft Foundation Classes; they're MS's C++ GUI class library.

VCL is the Visual Component Library; it's Borland's C++ GUI class library.

They compiled great on his box, but failed miserably on my MS Visual C++. What error did they fail miserably with?

JamminJoeyB
03-03-2003, 08:29 PM
Vegas/Newbie,

Post the errors you got. I deleted the email that had the errors.

BTW, I'm the one who got it to compile on linux.

Vegas/Newbie
03-03-2003, 09:48 PM
OK, after, these are the 5 errors I got. Hope that helps

----------
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.

new.obj - 5 error(s), 0 warning(s)

Vegas/Newbie
03-03-2003, 11:16 PM
MFC are the Microsoft Foundation Classes; they're MS's C++ GUI class library.

[B]VCL is the Visual Component Library; it's Borland's C++ GUI class library.

What exactly is the difference between the two?

I downloaded the trial of Borland C++ builder 6 and compiled my posted program and ran it. I got no errors and it ran fine.

I didn't think that there would be such a drastic difference between MS & Borland. I guess MS makes no reliable software!!

JamminJoeyB
03-03-2003, 11:39 PM
From the sounds of it, either the ms compiler is crippled or is not following the c++ specification.

I would bank on the latter. MS has a way of thinking that the whole world should do it the MS way.

Good to hear Borland got it compiled for you.

edit....


Whats the next lesson? Guess I better read up on c++

truls
03-04-2003, 04:26 AM
It will not work when you are including <string.h>. This is the old header, and is using the old-style strings. If you use <string> instead, which means you have to use std::string instead of just string, it will work.

So to further explain my first post (which was kind of meant to get you exploring ), you need to write something like:
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::cin;
using std::endl;

int main()
{
string name, num;
float g1, g2, g3, avg, ptsneeded;
cout << "Enter Student's Name: ";
cin >> name;
cout << "Enter Student's Number: ";
cin >> num;
cout << "Enter Grade 1: ";
cin >> g1;
cout << "Enter Grade 2: ";
cin >> g2;
cout << "Enter Grade 3: ";
cin >> g3;
avg = (g1+g2+g3)/3;
if (avg >= 65)
{
cout << name << " received a grade of S" << endl;
}
else
{
ptsneeded = 65 - avg;
cout << name << " received a grade of U" << endl;
cout << name << " would have needed " << ptsneeded;
cout << " more points to receive an S" << endl;
}
cin.get();
cin.get();
return 0;
}


Visual Studio 6 is actually pretty stable (what with the latest 100mb patch and all), and it is probably worth every cent you all payed for it :p (this being a comercial product and everything). Visual Studio .net on the other hand, ewwww....

spacedog
03-04-2003, 02:32 PM
whats up everyone,

I have been getting emailed about this post so im going to respond to the first problem... To get this code to run without errors on MSVC++ 6.0 do the following...everything works fine.
pay attention to the using namespace std; line thats what you were originally missing. Also if you put a .h at the end of the header files you will get some errors.

#include <iostream>
#include <string>


using namespace std;

int main()
{
string name, num; // this is the line I am having probs with. Is syntax/usage correct?
float g1, g2, g3, avg, ptsneeded;
cout << "Enter Student's Name: ";
cin >> name;
cout << "Enter Student's Number: ";
cin >> num;
cout << "Enter Grade 1: ";
cin >> g1;
cout << "Enter Grade 2: ";
cin >> g2;
cout << "Enter Grade 3: ";
cin >> g3;
avg = (g1+g2+g3)/3;
if (avg >= 65)
{
cout << name << " received a grade of S" << endl;
}
else
{
ptsneeded = 65 - avg;
cout << name << " received a grade of U" << endl;
cout << name << " would have needed " << ptsneeded;
cout << " more points to receive an S" << endl;
}
cin.get();
cin.get();
return 0;
}

Vegas/Newbie
03-04-2003, 04:34 PM
Thanks very much spacedog and truls. I have gained massive amounts of wisdom and my brain can now rest.

I am aware, though, that the .h in the header can be dropped, but "for class purposes" they must be included. However, I was unaware of the huge difference dropping the .h made. I added it then recompiled, and got 5 errors. Got none when I dropped it.

Again, I appreciate the help from everyone.

Until next project..........