9 Replies - 434 Views - Last Post: 03 June 2009 - 05:40 PM Rate Topic: -----

#1 nitrozach54  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-June 09

The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 02:40 PM

OK, so I have been reading up on C++, and have decided to make my first real program. Jeopardy.
The plan is to have 25 questions, a score, make it look nice, and have a clear, readable code.
Right now what is stumping me with my very limited programming knowledge, is how to use this board:


 //*****************************BOARD*********************************
cout << "Your score is currently: ";
cout << score << endl;
cout << "|===================================================|\n";
cout << "| History | Science| Movies | US Presidents | Misc. |\n";
cout << "|=========|========|========|===============|=======|\n";
cout << "|a. 100   |f. 100  |k	   |p.			 |u.	 |\n";
cout << "|_________|________|________|_______________|_______|\n";
cout << "|b. 200   |g. 200  |l	   |q.			 |v.	 |\n";
cout << "|_________|________|________|_______________|_______|\n";
cout << "|c. 300   |h. 300  |m	   |r.			 |w.	 |\n";
cout << "|_________|________|________|_______________|_______|\n";
cout << "|d. 400   |i. 400  |n.	  |s.			 |x.	 |\n";
cout << "|_________|________|________|_______________|_______|\n";
cout << "|e. 500   |j. 500  |o.	  |t.			 |y	  |\n";
cout << "|_________|________|________|_______________|_______|\n";

cout << " Please type the letter of the question you would like to answer: \n";
cout << " ";
cin >> bq; 



to be the "home screen" if you will. What I need to know is how to make the user input (a,b,c,d...) go to the corresponding place in the code( the question), and then return to that home screen after the question is done.

Could some one please tell me what I should do, or point me in the direction of a place that i can get the information needed to solve my problem?

EDIT: i am using dev c++

This post has been edited by nitrozach54: 03 June 2009 - 02:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: The has to be an easier way than a giant if and else statement!

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 02:45 PM

I don't really understand you question esp with the title -- can you expand on your question please?
Was This Post Helpful? 0
  • +
  • -

#3 nitrozach54  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-June 09

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 02:49 PM

If I type in "a" while running the program, I want it to take me to the question history for 100 points. I don't know how to do this without if or else.

This post has been edited by nitrozach54: 03 June 2009 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 03:02 PM

Ah, there are a number of ways to do this. One of them is with a if-else another is with a switch-case and another is with a loop...

The loop is probably the most complicated but probably the best. If you put your questions into an array then you can print out question number 10 as easy as accessing the 10'th element of that array...
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4881
  • View blog
  • Posts: 11,272
  • Joined: 16-October 07

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 03:12 PM

At the very least, you'll want an array. However, for easier to follow code, using a struct as well would be advisable.

In essence, all the places you have a letter and a number should have some kind of variable flag. Rather than just having variables a,b,c,d,e...z, it's much easier to have list[26], where list[0]==a, list[1]==b, etc.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 03:19 PM

Here is a little example I did once upon a time. I *think* it might be helpful.

I am sure there are some better examples somewhere here...
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 03:39 PM

yea ignore that example. Not really all that helpful for what you are doing. Might be ok to look at but does not really apply.
Was This Post Helpful? 0
  • +
  • -

#8 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 04:02 PM

Okay.. :ph34r: so after you answer a question you want the a.100 to disappear once its used up like in real Jeopardy, right? Store them in a separate vector or array or struct than the design of the board and reprint the whole thing each loop BUT...replace the vector/struct index with enough blank spaces to keep the structure of the board's walls.

Like...
//pseudo-code

cout << "/   " << history.a_question << "	 /" << etc........... 


//then after its answered


cout << "/   " << history.a_blankspace << "	 /" << etc...........




So when after its gone, the right amount of spaces are still keeping the whole home board intact.

As far as the loop, just clear screen and reprint the new board after each question or something.

This post has been edited by Kanvus: 03 June 2009 - 04:04 PM

Was This Post Helpful? 1
  • +
  • -

#9 nitrozach54  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-June 09

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 04:53 PM

Can someone please point me twords a page that gives some simple, clear, detailed explanation of that with examples?
oh yeah and I might want to make the questions disappear eventually, but thats after I have completed the program.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4881
  • View blog
  • Posts: 11,272
  • Joined: 16-October 07

Re: The has to be an easier way than a giant if and else statement!

Posted 03 June 2009 - 05:40 PM

Rather than try to explain, I'm going to offer a proof of concept. This uses loops, structs, arrays, pass by reference, and basically all the elements you'd expect to find in a basic C++ program. If any of these things are new to you, you're going to have to take a look at that book.

#include <iostream>

using namespace std;

const int maxItems = 15;

struct Game {
	int score;
	bool items[maxItems];
	Game() : score(0) { for(int i=0; i<maxItems; i++) { items[i] = true; } }
};


void showCurrent(Game &game) {
	cout << "Your score is currently: ";
	cout << game.score << endl;
	cout << "|=============================|\n";
	cout << "| History | Science | Movies  |\n";
	cout << "|=========|=========|=========|\n";
	int rows=5, cols=3;
	for(int row=0; row<rows; row++) {
		int rowVal = (row+1)*100;
		for(int col=0; col<cols; col++) {
			int index = (col*rows) + row;
			if (game.items[index]) {
				char ch = (char)('a' + index);
				cout << "| " << ch << ". " << rowVal << "  ";
			} else {
				cout << "|         ";
			}
		}
		cout << "|\n";
		for(int col=0; col<cols; col++) { cout << "|_________"; }
		cout << "|\n";
	}
}


int main() {
	Game game;
	game.items[0] = false;
	game.items['i'-'a'] = false;
	showCurrent(game);
	return 0;
}



Results.
Your score is currently: 0
|=============================|
| History | Science | Movies  |
|=========|=========|=========|
|		 | f. 100  | k. 100  |
|_________|_________|_________|
| b. 200  | g. 200  | l. 200  |
|_________|_________|_________|
| c. 300  | h. 300  | m. 300  |
|_________|_________|_________|
| d. 400  |		 | n. 400  |
|_________|_________|_________|
| e. 500  | j. 500  | o. 500  |
|_________|_________|_________|



I really hope this helps. If it's overwhelming, maybe it will at least give you some place to start.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1