Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,345 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,632 people online right now. Registration is fast and FREE... Join Now!




char to int

 
Reply to this topicStart new topic

char to int

dewjunkie
post 14 Mar, 2008 - 04:37 PM
Post #1


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 49

Hey all!

Ok so here's my problem:

The function that I am writing accepts 2 user inputs, one char and then one int. (one int one char assignment instructions)

However, I want to allow the user the ability to enter them in either order (1 char 2 int or 1 int 2 char ).

I have code that does allow me to accept both inputs in either order. So where's my problem?

My problem is the inputs the user is entering coincide to a location in a multidimensional array. I think there's an issue with the line of code that I set an int to a char (You all probably are lost, look for the stars in my code, this is where I think my problems are happening).


Maybe my problem is in the way I am accepting both inputs, but I can't think of another way. Any suggestions anyone might have for me would be greatly appreciated!!!!!

CODE


#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;

int main ()
{
            

int grid[5][5] =
{
{ 35, 0, 90, 50, 75},
{ 55, 10, 0, 0, 20 },
{ 0, 85, 40, 95, 60 },
{ 15, 0, 65, 5, 100 },
{ 70, 45, 25, 80, 30 }
};

char gridLocLetter;
int gridLocNumber;  

cout << "Enter a grid location (such as B2, or 2b)";
cin >> gridLocLetter;

  if( isalpha(gridLocLetter) != 0 )
  {  
   cin >> gridLocNumber;
  }

   else if (isdigit(gridLocLetter) != 0)
************************************************
*   { gridLocNumber = gridLocLetter;                             *
************************************************
   cin >> gridLocLetter;
   }

cout <<"GLL: " << gridLocLetter << " GLN: " << gridLocNumber << "\n";

cout << "Grid location: " << gridLocLetter << gridLocNumber << "\n";
int X;

if (gridLocLetter == 'A'|| gridLocLetter == 'a')
X = 0;

else if (gridLocLetter == 'B'|| gridLocLetter == 'b')
X = 1;

else if (gridLocLetter == 'C'|| gridLocLetter == 'c')
X = 2;

else if (gridLocLetter == 'D'|| gridLocLetter == 'd')
X = 3;

else if (gridLocLetter == 'E'|| gridLocLetter == 'e')
X = 4;

else
cout <<"Input error: letter range is A-E, number range is 1-5.\n";

cout << "X is: "<< X;

cout << grid[X][gridLocNumber-1] <<" ";
cout << "\n";
cout << "Item " << gridLocLetter << gridLocNumber << " costs " << "2" << " cents.\n"

           system("PAUSE");
           return 0;
}

User is offlineProfile CardPM

Go to the top of the page


KYA
post 14 Mar, 2008 - 09:47 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,148



Thanked 47 times

Dream Kudos: 1150
My Contributions


Enumerate A, B, C, D, E (I'm assuming that is your letter use for the columns). So then if the user enters an 'A', the program will read it as a one:

CODE

enum {A = 1, B, C, D, E}
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/19/08 04:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month