C++: VS98 and Strings

mavalpha

[H]F Junkie
Joined
Jan 3, 2005
Messages
10,448
I'm trying to turn a string input into a linked list as per a programming assignment, and it's spitting out a few errors, all of which seem to be string-related. To test a hunch, I wrote a stupidly simple test program:
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
	string a="Hello world.";
	
	cout<<a;
	getch();
}
According to this site, I have all of the right Include files, and my syntax seems fine.
Compile turns up four errors, which are all caused by the initial String failure:
'string' : undeclared identifier
syntax error : missing ';' before identifier 'a'
'a' : undeclared identifier
'=' : cannot convert from 'char [14]' to 'int'
I'm beginning to think one of two things happened:
1) My VS98 install is borked
2) This is something that changed after MS Visual Studio/C++ 6.0

Can anyone help?
 
Tried both, same result. I was thinking of putting that in my original post. :)

They both turn up the exact same errors, btw.
(I did find the difference here.)
 
As Lord of Shadows said you need to include <string> (not <string.h>) but you must also include the std namespace.
 
Code:
#include<iostream.h>
#include<conio.h>
#include<string>
using namespace std;

void main()
{
	string a="Hello, World.";
	
	cout<<a;
	getch();
}
1 error:
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 ther
e is no acceptable conversion)

Edit:
Wow, I can't remember the last time I wrote a program that actually made use of namespace. :eek: I dropped it out of force of habit.
 
Code:
#include <iostream>
#include <string>

using namespace std;

void main() {
    string strTest = "Hello, World";
    
    cout << strTest;
    getchar();
}

Works fine for me...
 
Okay, that did it. iostream.h (C) can't handle strings, but iostream (C++) can.
 
Wow, I can't remember the last time I wrote a program that actually made use of namespace. :eek: I dropped it out of force of habit.
Habit? Well, hopefully you drop this bad habit ASAP. You need to call functions within a namespace either through the using statement or qualifying the function call with the namespace::function format.
Code:
#include <iostream>
#include <string>

using namespace std;

void main() {
    string strTest = "Hello, World";
    
    cout << strTest;
    getchar();
}

Works fine for me...

main() returns int, not void.
Okay, that did it. iostream.h (C) can't handle strings, but iostream (C++) can.

iostream.h is not C. iostream is the deprecated way to including the iostream library from the STL in C++. You should always include STL via the #include <library> method (i.e., dropping the .h) or c libraries by dropping the .h and putting c in front of it (i.e. #include <stdlib.h> to #include <cstdlib>).

Are you learning this in school or something? If so, your professor shouldn't be teaching C++. You're learning a lot of bad habits and your code looks like it wants to be C.

Why are you using VS98? Why can't you use the free VisualC++ Express? It's more standards compliant with the C++ standard.
 
iostream.h is not C. iostream is the deprecated way to including the iostream library from the STL in C++. You should always include STL via the #include <library> method (i.e., dropping the .h) or c libraries by dropping the .h and putting c in front of it (i.e. #include <stdlib.h> to #include <cstdlib>).
This is good advice, except that iostream isn't part of the STL; it's a part of the Standard C++ library. And the operand for the #include directive is a header, not a library.
 
Are you learning this in school or something? If so, your professor shouldn't be teaching C++. You're learning a lot of bad habits and your code looks like it wants to be C.

Why are you using VS98? Why can't you use the free VisualC++ Express? It's more standards compliant with the C++ standard.
I have my own copy of VS98, and a beta of VS2005. I just never got around to installing '05 Beta. I've been learning it since high school, which was around 1999.
My current professor has a PhD in either Fortran or Pascal, and is learning C+ as he is teaching it to us. :(
main() returns int, not void.
What's wrong with void main()? I've now had three professors tell me that int main() is preferred, but void is acceptable as well. The main body of my program isn't supposed to return anything, making it suitable for a void declaration.
 
mavalpha said:
What's wrong with void main()? I've now had three professors tell me that int main() is preferred, but void is acceptable as well. The main body of my program isn't supposed to return anything, making it suitable for a void declaration.

void main() is not a part of the C++ standard, but most compilers support its use because of older C and C++ code that use the convention, just like how the .h versions of the C++ standard library like iostream.h are 'deprecated', but still supported. There may be some compilers that don't allow void main(), but I couldn't tell you which ones.

In Unix-like environments, the environment expects a return value from the program, whereas in something like a Windows environment, I believe the shell does not expect a return value indicating success.

However, I believe most compilers (or maybe this is part of the C++ standard, I don't have the standard here for reference) inject a 'return 0' statement to main if you don't explicitly add the line.
 
In Unix-like environments, the environment expects a return value from the program, whereas in something like a Windows environment, I believe the shell does not expect a return value indicating success.
Windows, like Unix, just expects an integer back. The value doesn't indicate success or failure, though by convention, people use 0 to indicate success. The OS doesn't care about the value at all.

However, I believe most compilers (or maybe this is part of the C++ standard, I don't have the standard here for reference) inject a 'return 0' statement to main if you don't explicitly add the line.

Not quite. The standard says that you have to use int as a return unless you're writing for an embededd system (essentially). It also explains that, if you leave main() without executing a return function, then it's as if you've used return 0;. This is a little bit different than giving main the wrong return prototype.

I'm always amused at how this forum responds to main implemenations that don't return int. It's probably one of the most inconcequential mistakes a C or C++ programmer can make, but it draws comments without fail.
 
In bash (and I'm sure it's true of other *nix shells) the return value is set as the environment variable $?, which can be tested with scripts etc. It also is returned as an int from the system() call. I think I remember mike mentioning that windows just ignores the number completely, although I havent looked myself. -- I think thats about it when it comes to uses you may actually encounter.
 
Windows ignores the number, but it's available to whoever executed the process by calling the GetExitCodeProcess() API.

In a CMD script, you can look at it with %ERRORLEVEL%.

The point is that it doesn't mean success or failure as far as the OS is concerned, as main doesn't succeed or fail by any useful definition. Various de facto standards exist -- usually with 0 meaning success and anyhting else meaning some sort of failure.
 
Okay, how does this look?
Code:
#include <iostream>
#include <string>
using namespace std;

string charcopy(char *tempstring)    //Inputs a character array (including spaces), converts into a string
{
	string finalstring;
	
	cin.getline(tempstring, 100);
	
	for(int tempindex=0;tempindex<100;tempindex++)
	{
		finalstring+=tempstring[tempindex];
	}
	return finalstring;
}

struct node
{
	int charindex;
	char letter;
	node* next;
};

void makeList(node *head, string phrase)     //Converts a string into a linked list, one letter per node
{
	node *current;
	current = head;
	for(int index=0;index<phrase.length();index++)
	{
		current->letter=phrase[index];
		current->charindex=index;
		current=current->next;
		current=new node;
	}
	delete current;
	current->next=NULL;
}

void showList(node *head)
{
	node* current;
	current = head;
	do{
		cout<<current->charindex<<" "<<current->letter<<endl;
		current=current->next;
	}while(current->next!=NULL);
}

int main()
{
	char char_letters[100];
	cout<<"Please input a test string.\n";
	string letters=charcopy(char_letters);
	node *Head1; 
	Head1 = new node;
	makeList(Head1,letters);
	showList(Head1);
	
	return 0;
}
I'm still getting the hang of linked lists, so I'm pretty sure that's where it's crashing now.
 
I'm not a C++ guy, but you might want to pick a coding style in programming and be consistent with it. Your local variables appear to sometimes be pascal case and sometimes you keep it lower case.

In C#, I try to follow some of the popular coding styles which involve:
  • Pascal case on class names
  • Camel case on private members/methods
  • Pascal case on public members/methods
  • Camel case on local variables
  • Upper case on constants
It doesn't matter so much as to what way you do it, but it does make it easier to read.
 
I'm not a C++ guy, but you might want to pick a coding style in programming and be consistent with it. Your local variables appear to sometimes be pascal case and sometimes you keep it lower case.

In C#, I try to follow some of the popular coding styles which involve:
  • Pascal case on class names
  • Camel case on private members/methods
  • Pascal case on public members/methods
  • Camel case on local variables
  • Upper case on constants
It doesn't matter so much as to what way you do it, but it does make it easier to read.
Okay, I'll have to keep that in mind. Usually I capitalize for important variables/functions, like Head1 in this case- it's the primary index to the major list in my program. I see what you mean, though, in terms of composite names like makeList/showList vs. char_letters. I don't use constants too often, but capitalizing them makes sense as well.

What are Pascal and Camel case?
 
What are Pascal and Camel case?

Pascal case is initial capitalization on every word and Camel case is initial capitalization on every word except for the first.

Pascal case:
  • class MyClass { }
  • class Employee { }
  • public int Length { get; set; }
  • public int GetLength() { }

Camel case:
  • private int m_Length;
  • private int calcLength() { }

There are many other styles that others use. Some programmers use underscores to separate words, some use hungarian notation. Also, my programming style in C# is different than the programming style I use in C++.
 
Back
Top