If Else Statements Help

Need a little help with If Else Statements

Page 1 of 1

2 Replies - 827 Views - Last Post: 14 September 2009 - 11:49 PM Rate Topic: -----

#1 Dudepop34   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-September 09

If Else Statements Help

Post icon  Posted 14 September 2009 - 06:27 PM

Thank you another member helped me out and I found out that there is a much simpler solution to this.

Could a mod or admin please close this thread.


#include "iostream"
#include "string"
#include "sstream"

using namespace std;

struct contacts_t{
	int age;
	int number_p;
	string address;
}joe,bob;

void printmessage(contacts_t contact);

int main()
{
	string mystr;
	string mystrt;
	char a;
	string s="s";
	string b="b";

	cout<<"Please enter the name of the contact you wish to edit: (Susie, Bobby) ";
	getline(cin,mystr);
	stringstream(mystr)>> a;

	char * name;
	name = new (nothrow) char [a];

	if (name==0)
		cout<<"Error: Memory could not be allocated.\n";

	else if (name[0]=='s')
		cout<<name[0]<<endl;

	else if (name[0]=='b')
		cout<<name[0]<<endl;

	else
		cout<<"Fail\n";

	system("PAUSE");
	return 0;
}

void printmessage(contacts_t contact){
	cout<<"Eh.";
}



There is no error, however when I type in joe, or bob, it always goes directly to the "Fail".
Can anyone tell me what i'm doing wrong?

This post has been edited by Dudepop34: 14 September 2009 - 06:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: If Else Statements Help

#2 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: If Else Statements Help

Posted 14 September 2009 - 06:33 PM

Here is an error
 if (name==0)//you cannot compare a char to an integer

Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: If Else Statements Help

Posted 14 September 2009 - 11:49 PM

why are you using standard header files inside of your directory. These headers are precompiled directives that are already apart of the system. Use <> instead of quotes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1