Mar 10, 2012 at 11:58am UTC
this is an example program from Robert Lafore OOP in C++ book,but its not working in Visual Studio 2010, can anyone please help me out what I'm doing wrong?
#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
class String{
private:
enum{SZ=80};
char str[SZ];
public:
String(){
strcpy(str," ");
}
String(char s[]){
strcpy(str,s);
}
void display() const{
cout<<str;
}
void getstr(){
cin.get(str,SZ);
}
bool operator==(String ss) const{
return(strcmp(str,ss.str)==0)?true:false;
}
};
void main(){
//system("CLS");
String s1="yes";
Sting s2="no;
String s3;
cout<<"\nEnter 'yes' or 'no':";
s3.getstr();
if(s3==s1)
cout<<"You yped yes\n";
else if(s3==s2)
cout<<"You typed no\n";
else
cout<<"You didnt follow instructios\n";
getch();
}
Mar 10, 2012 at 12:26pm UTC
What does mean "it is not working"?! Why somebody should make guesses about what is not working?!
I see that at least you forgot to place " after s2 in statement Sting s2="no;
Mar 10, 2012 at 12:45pm UTC
In addition to the missing quote, you also wrote Sting instead of String on the same line.
I corrected that in your code and I believe you designed it (Except for a spelling mistakes).
You should listen to your compiler when it gives you an error. It will tell you things way faster than we can:
This was the first error I got when compiling:
1>Snippets.cpp(31): error C2065: 'Sting' : undeclared identifier
That means that you have misspelled String. When I fixed that I got:
1>Snippets.cpp(31): error C2001: newline in constant
1>Snippets.cpp(32): error C2146: syntax error : missing ';' before identifier 'String'
That tells us that there was something funny with the string in line 31 and that we are missing a ; (which is true since it is in a quote).
Always look at the first errors first, Fixing those will often cause other errors which were numerous to not appear at all.
Last edited on Mar 10, 2012 at 12:46pm UTC
Mar 10, 2012 at 4:16pm UTC
Thanks Stewbond and vlad too.. I should have tried to understand the errors..