Support Forums

Full Version: Help Please. Newbie at work. Failing.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey.
So, I'm new to C++. I thought why not try and get into OOP. I don't think I quite understood how they work.
My program is supposed to work like an ATM machine. First off it should check user input for a name. And that's where I fail already. I just can't get it to work and I have no idea why.
The errors are i.e : ISO C++ forbids initialization of member `Name' ´; making `Name' static ; invalid in-class initialization of static data member of non-integral type `std:Confusedtring' ;


Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;


class User
{
      public:
      string Name = 'Bob Kelso' ;
      
      
      string Check(string x)
      {
           string com = Name;
           if ( com == x)
           {
                cout << "You made it. Congratz" << endl;
           }
      }
      
};

int main()
{
    
  string x;
  
  cout << "Enter your name to withdraw money : " ;
  cin >> x;
  cin.ignore();
  
  Check(x);
  
  
  cin.get();
  return 0;
    
}
Thanks in advance Thumbsup
who has two thumbs and doesnt give a crap....bob kelso..have we met.
(10-07-2009, 01:22 PM)Code King Wrote: [ -> ]who has two thumbs and doesnt give a crap....bob kelso..have we met.

Sorry to spam your thread, but that was full of win. Blackhat

/Vorfin Out
<sarcasam>wow Code King and Vorfin you are really helpful... </srcasam>

@Topic

You didn't initialize the class, take a look at the first two lines in main() and how the Check() is called, and the variable Name shouldn't be defined within the class
And also your function Check is set to return a string "string Check()" but it doesn't return a string wich may cause app to shutdown, instead of printing out the success message in Check() you should return it and cout when Check() is called

Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;


class User
{
      public:
      string Name;
      
      
      string Check(string x)
      {
           string com = Name;
           if ( com == x)
           {
                return "You made it. Congratz";
           }
      }
      
};

int main()
{
  User *user = new User();
  user->Name = "Bob";
  string x;
  
  cout << "Enter your name to withdraw money : " ;
  cin >> x;
  cin.ignore();
  
  cout << user->Check(x) << endl;
  
  
  cin.get();
  return 0;
    
}
(10-07-2009, 01:22 PM)Code King Wrote: [ -> ]who has two thumbs and doesnt give a crap....bob kelso..have we met.

QFT Yeye



(10-07-2009, 01:59 PM)NinjaGeek Wrote: [ -> ]<sarcasam>wow Code King and Vorfin you are really helpful... </srcasam>

@Topic

You didn't initialize the class, take a look at the first two lines in main() and how the Check() is called, and the variable Name shouldn't be defined within the class
And also your function Check is set to return a string "string Check()" but it doesn't return a string wich may cause app to shutdown, instead of printing out the success message in Check() you should return it and cout when Check() is called

Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;


class User
{
      public:
      string Name;
      
      
      string Check(string x)
      {
           string com = Name;
           if ( com == x)
           {
                return "You made it. Congratz";
           }
      }
      
};

int main()
{
  User *user = new User();
  user->Name = "Bob";
  string x;
  
  cout << "Enter your name to withdraw money : " ;
  cin >> x;
  cin.ignore();
  
  cout << user->Check(x) << endl;
  
  
  cin.get();
  return 0;
    
}

When I use your code, it stills gives me an error :S
Error : In member function ‘std:Confusedtring User::Check(std:Confusedtring)’:
AND
warning: control reaches end of non-void function

???
It works though, thanks. Kind of weird output when I enter the wrong name, but still Big Grin
(10-08-2009, 08:19 AM)TheDoctor Wrote: [ -> ]When I use your code, it stills gives me an error :S
Error : In member function ‘std:Confusedtring User::Check(std:Confusedtring)’:
AND
warning: control reaches end of non-void function

???
It works though, thanks. Kind of weird output when I enter the wrong name, but still Big Grin

I don't get that error, but you can avoid the error by changing string Check() to int Check() and make it return true or false

Code:
int Check(string x)
      {
           string com = Name;
           if ( com == x)
           {
                return 1;
           }else{
                return 0;      
           }
      }


Then you call Check within an if condition
Code:
if(user->Check(x))
  {
      cout << "You made it. Congratz" <<endl;
  }

If the function returns true you output the message


http://www.ibm.com Wrote:This warning is similar to the warning described in Return with no value. If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.
Cheers mate Big Grin
(10-08-2009, 10:23 AM)TheDoctor Wrote: [ -> ]Cheers mate Big Grin
I'm glad I could help
Blackhat
(10-07-2009, 01:28 PM)Vorfin Wrote: [ -> ]Sorry to spam your thread, but that was full of win. Blackhat

/Vorfin Out

haha WINSuperman
You need to have a return type in the String Function it isn't a void.

Cheers! Good Luck, nice to see you in the forum
Pages: 1 2