|
 |
|
 |
10-18-2005, 09:20 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 2
|
Need help
Hey everyone, this is my first post, and i was wondering if you could lend a hand with a problem i'am having with a class project. Im stuck on what to do. Any help would be greatly appreceited.
here is the project guidelines:
Description :
Write a program to read in a set of integer starting coordinates. Then read in a series of "directional moves". Each directional move is made up of a single character followed by a real number. The char is N, S, W, or E to indicate direction and the real number is how far to "move". When the user enters a 'q' for a direction the program will then print out the final location. (Do not allow further input)
Input :
First, a set of integers representing a starting location (X, Y). Then a series of directional moves as described above until a 'q' is entered. Case does not matter to the user.
Output :
Output is the final (X,Y) coordinates up to 2 decimal places. Account for the spaces as show in the sample run.
Sample Run :
Here is a sample execution:
% ./project2
Welcome to Project #1 : You Sunk My Battleship!
Enter starting (X, Y) coordinates : 12 5
Enter Move : N 5.0
Enter Move : S 3.3
Enter Move : E 13.3
Enter Move : W -2.33
Enter Move : Q
The final position is ( 27.63, 6.70 ).
%
Here is another sample run, more complicated:
% ./project2
Welcome to Project #1 : You Sunk My Battleship!
Enter starting (X, Y) coordinates : 12 5
Enter Move : N 3.4
Enter Move : S -2.6
Enter Move : E 5
Enter Move : w 9
Enter Move : w -3.2
Enter Move : w 0.1
Enter Move : e -0.33
Enter Move : n 5.12
Enter Move : s -3.141592654
Enter Move : q
The final position is ( 10.77, 19.26 ).
%
Here is ths actual code i have thus far. I' am very new to coding and any help would be greatly appreceited.
/////////////////////////////////////////////
#include <iostream>
void main () {
int x,y;
char dir;
cout << "Enter strating Coordinates (X, Y) :";
cin >> x, >> y;
do {
cout << "Enter Move :";
cin >> dir;
} while (dir !='q');
}
///////////////////////////////////////////////////
I know it is very raw, but i have no clue what to do from here. thank you again. If you have a better idea to write this code, please let me know and show.
Thanks
Robert
|
|
|
10-18-2005, 10:00 PM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
Has your class studied functions, yet? At any rate, after you get the starting coords maybe you could store them in float variables called X and Y.
Each time you loop though the input, you should take in a char and use a switch statement. If the char is q, cout << X, Y, or if it is a direction (N,E,S,W) read a float.
The direction itself will tell you what to do with the float. N for example means you should add that float to Y, whereas W, would mean subtract it from X.
Does that outline help enough that you could write the code now?
__________________
Stop intellectual property from infringing on me
|
|
|
10-18-2005, 11:45 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 2
|
not really, think you could write up an example. sorry bro, i just dont get it yet.
|
|
|
10-19-2005, 12:30 AM
|
#4 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
If I write up an example I've done your work for you, "bro." Why don't you write a bit more and post how much farther you've gotten.
__________________
Stop intellectual property from infringing on me
|
|
|
10-19-2005, 02:09 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Study this code closely then
Code:
#include <iostream>
int main()
{
double x=0.0,y=0.0, incr=0.0;
char move;
std::cout << "Enter starting Coordinates (X, Y) : ";
std::cout.flush();
std::cin >> x >> y;
std::cin.ignore(); /* discard the '\n' in stdin stream */
while(move != 'q' && move != 'Q')
{
std::cout << "Enter Move : ";
std::cout.flush();
std::cin >> move;
if(move == 'q' || move == 'Q')
break;/* we gotta go */
std::cin >> incr;
std::cin.ignore();
switch(move)
{
case 'n':
case 'N':
/* move north, increase y */
break;
case 'e':
case 'E':
/* move east, increase x */
break;
case 's':
case 'S':
/* move south, decrease y */
break;
case 'w':
case 'W':
/* move west, decrease x */
break;
default:
std::cout << "The chosen move (" << move
<< ") is unsupported" << std::endl;
break;
}
}
std::cout <<"The final position is (";
std::cout << x << ", " ;
std::cout << y << ")" << std::endl;
return 0;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:12 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|