LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-10-2003, 03:40 PM   #1
oceaneyes2
LQ Newbie
 
Registered: Dec 2003
Location: Maryland
Posts: 7

Rep: Reputation: 0
Arrow switch statement converting struct char to struct int


is there any way to make this work? If not how should i convert char to int when defined in a struct?

switch (char Employee[i].payCode)
{
case 'A': case 'a':
Employee[i].pay = 20;
break;

case 'B': case 'b':
Employee[i].pay = 15;
break;

case 'C': case 'c':
Employee[i].pay = 10;
break;

default
cout << "Invalid";
}
 
Old 12-10-2003, 04:21 PM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Based on your previous post, I'm guessing payCode is defined in the struct as:

char payCode[2];

This is a char array, which cannot be used in a switch statement.

However, it appears from your case statements that you ONLY care about the first character of that. So...

Why not define it as char payCode instead? Either that or you could do the switch on Employee[i].payCode[0] instead...

Also, you have a char in that switch, which you probably don't want. You are trying to declare a new char variable named Employee[i].payCode[0], which is not only not what you want, it is also probably giving you compiler errors.
 
Old 12-10-2003, 04:30 PM   #3
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Hi oceaneyes2,

First off there are a couple of small errors in your code as highlighted here in red:

Code:
switch(char Employee[i].payCode) // Point 1
{
  case 'A': case 'a':
    Employee[i].pay = 20;
    break;

  case 'B': case 'b':
    Employee[i].pay = 15;
    break;

  case 'C': case 'c':
    Employee[i].pay = 10;
    break;

    default  // Point 2
      cout << "Invalid";
}
1) The typename char is not needed here.
2) There is a colon (':') missing from the default keyword.

Here is some correct code.

Code:
struct employeeT
{
  char payCode;
  int pay;
};

int main()
{
  employeeT Employee;  

  doStuffWithEmployee(Employee);
  
  switch (Employee[i].payCode)
  {
    case 'A': 
    case 'a':
      Employee[i].pay = 20;
      break;

    case 'B':
    case 'b':
      Employee[i].pay = 15;
      break;

    case 'C': 
    case 'c':
        Employee[i].pay = 10;
        break;

    default:
       cout << "Invalid";
  }
}
Or even more simple (yet possibly less efficient)

Code:
#include <ctype.h> // for toupper

struct employeeT
{
  char payCode;
  int pay;
};

int main()
{
  employeeT Employee;  

  doStuffWithEmployee(Employee);
  
  switch (toupper(Employee[i].payCode))
  {
    case 'A': 
      Employee[i].pay = 20;
      break;

    case 'B':
      Employee[i].pay = 15;
      break;

    case 'C': 
        Employee[i].pay = 10;
        break;

    default:
       cout << "Invalid";
  }
}
Hope this helps..

Cheers..
 
  


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
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM
problems copying int a struct raven Programming 8 07-21-2002 10:10 AM

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

All times are GMT -5. The time now is 06:15 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