LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2008, 01:34 AM   #16
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148

you may want to try adding a \r whenever you have a \n.
 
Old 09-05-2008, 10:17 AM   #17
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
The following will work fine using only the standard library.
Code:
#include <iostream>

void print_question(int const& x,int const& y)
{
	std::cout << "\nYour coordinates are: \n" << x << ", " << y << ".\n"
	<< "Enter the direction you like: (n,s,e,w)" <<std::endl;
}

int main()
{
	char dir;
	int x = 10, y = 10;
	print_question(x,y);

	while (std::cin.peek() != '\n')
	{
		std::cin >>dir;
		std::cin.ignore(10000 , '\n');
		switch(dir)
		{
			case 'n': y++; break;
			case 's': y--; break;
			case 'e': x++; break;
			case 'w': x--; break;
			default: std::cout << "You made a mistake! Try again " <<std::endl;
		}
		print_question(x,y);
	}
	std::cout <<"Exiting the program, bye bye!" <<std::endl;
	return 0;
}
 
Old 09-06-2008, 12:19 AM   #18
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
Hi All,

dmail, your code worked fine. I am studying it.
I have a couple of questions:
Why did you have
Code:
void print_question(int const& x,int const& y)
{
	std::cout << "\nYour coordinates are: \n" << x << ", " << y << ".\n"
	<< "Enter the direction you like: (n,s,e,w)" <<std::endl;
}
outside main() function? And why not use
Code:
using namespace std;
(I know it might be your way of writing codes, I just want to make sure I follow everything here.

And Is this the solutions to conio.h problem in Linux?

As I said I am using Robert Lafore's book (Object Oriented programming using C++) from Waite Group, 3rd edition. And it seems he wrote it for Windows. I love the book but I have to find a solution to this conio.h problem.


A small note:
Whenever Microsoft introduces something, it just makes life more difficult. If they can't play by the rules (ISO/ANSI) they should quit!

faizlo
 
Old 09-06-2008, 12:30 AM   #19
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
graemef, I have tried replacing \n with \r and I got the first line (asking about the direction) and nothing happens whenever I press n,e,s,w. but it quits when I use q.

faizlo
 
Old 09-06-2008, 05:32 AM   #20
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by faizlo View Post
Hi All,

dmail, your code worked fine. I am studying it.
I have a couple of questions:
Why did you have
Code:
void print_question(int const& x,int const& y)
{
	std::cout << "\nYour coordinates are: \n" << x << ", " << y << ".\n"
	<< "Enter the direction you like: (n,s,e,w)" <<std::endl;
}
outside main() function?
The reason I included the printing of the question in a function is that it is called in two different places in the code I provided, it would be redundant to write the code twice.
Quote:
And why not use
Code:
using namespace std;
(I know it might be your way of writing codes, I just want to make sure I follow everything here.
I could have used the using declaration local to main (global namespace pollution is bad and should be avoided) yet it is as you say just a preference and how long does it take to type "std::"? When working with a large codebase it makes it explicit what you mean, as there could be code in different namespaces which have structures of the same name.

Quote:
And Is this the solutions to conio.h problem in Linux?
The problem is the author is using windows only functions, in my opinion this is wrong as he is meant to be teaching C++.

Last edited by dmail; 09-06-2008 at 05:35 AM.
 
Old 09-06-2008, 05:56 AM   #21
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
dmail,

Thank You.

faizlo
 
Old 09-06-2008, 06:27 AM   #22
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by faizlo View Post
graemef, I have tried replacing \n with \r and I got the first line (asking about the direction) and nothing happens whenever I press n,e,s,w. but it quits when I use q.

faizlo
I meant add not replace I believe that within ncurses \n is move to the next line but keep the same column whilst \r is move to the first column. However, dmail's solution is much neater.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getchar() problem. Read other threads. jstephens84 Programming 3 11-20-2005 04:10 PM
conio.h?? jojopopo0372 Programming 3 09-07-2004 03:11 AM
conio.h ??? Umanga Programming 3 09-02-2004 05:06 PM
conio.h matt_w_lambert Programming 9 07-19-2003 12:04 AM
Conio Mohsen Programming 6 02-19-2003 02:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration