2 Replies - 276 Views - Last Post: 25 October 2013 - 02:16 AM Rate Topic: -----

#1 farzad021  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-October 13

recognizing relation in console c++ without external database(sql,...)

Posted 25 October 2013 - 12:42 AM

hello every one
I'm working on a basic console app in C++
user have a list of people that contains 13 names,user should enter one of the names of the list and the app should show user a relation status
(one of the names in the list is jack,we want the app to tell us the 12 other people in the list have what kind of relation with jack,for example,if user entered the jack,the app show us "Himself" or if entered sara,show us "his mother")
this is the list: (jack,sara,john,walt,rose,kate,arnold,james,taylor,sawyer,jimmy,nikita,fedal)
when in the app I enter john,the app shows me the "niece or father",but when i start entering the second name up to 13th name,the app shows me the "error" , in fact just first define works
this is my code:
#include <set>
#include <iostream>
#define john a
#define sara b
#define walt c
#define rose d
#define kate e
#define jack f
#define james g
#define taylor h
#define sawyer i
#define arnold j
#define jimmy k
#define nikita l
#define fedal m

using namespace std ;


void main() {
	char entname;
	cout << "Please enter the name:";
	cin >>  entname;
    switch ( entname )
	{
	case 'a': 
		cout << "father or nephew" << "\n";
		break;

	case 'b':
		cout << "mother";
		break;

	case 'c':
		cout << "brother";
		break;

	case 'd':
		cout << "niece";
		break;

	case 'e':
		cout << "sister";
		break;

	case 'f':
		cout << "HIMSELF";
		break;

	case 'g':
		cout << "nephew";
		break;

	case 'h':
		cout << "sister";
		break;

	case 'i':
		cout << "nephew";
		break;

	case 'j':
		cout << "His son";
		break;

	case 'k':
		cout << "brother";
		break;

	case 'l':
		cout << "niece";
		break;

	case 'm':
		cout << "nephew";
		break;
	default:
		cout << " error " << "\n";
	}
		
}



I dont know what is wrong ...
thank you

Is This A Good Question/Topic? 0
  • +

Replies To: recognizing relation in console c++ without external database(sql,...)

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: recognizing relation in console c++ without external database(sql,...)

Posted 25 October 2013 - 12:58 AM

Change your defines to ALL uppercase letters only. Change the case statements to uppercase letters:

case 'A': etc.

Change HIMSELF to all lowercase letters.

The preprocessor does a substitution of every define, before the program is compiled. You've got your lowercase letters in the defines, crapping on your program, BADLY.

One letter defines even with uppercase letters, are risky if you EVER use an uppercase letter in your program - just so you know.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: recognizing relation in console c++ without external database(sql,...)

Posted 25 October 2013 - 02:16 AM

I wouldn't use a switch. (Ever, honestly.) This is C++? You have strings! Use them.
e.g.
	string entname;
	cout << "Please enter the name:";
	cin >>  entname;
	
	if (entname=="john") {
		cout << "father or nephew";
	} else if (entname=="sara") {
		cout << "mother";
	//...



However, those names should really be in an array so you can loop through them and show them to the user. If you have another array with the responses... or a struct with name and response...

If you've learned how to use arrays yet, you should be using them now.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1