Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 16, 2005, 11:49 AM   #1
Sirena400
macrumors newbie
 
Join Date: Feb 2005
C++ help?

Hello all, I got a new problem to do in copmuter programming and I can't seem to figure out how to correct the error in my programming. In the program I will be given a zulu time(aja, Greenwich[England] mean time) and an indication of the US time zone I'm in. E will be Eastern-Standard, C for Central-Standard, M for Mountain-Standard time, and P for Pacific-Standard time. I'm supposed to produce the local standard time that would appear on a 12 hour clock for the indicated time zone and given zulu time. along with the appropriate a.m. or p.m. designation. Zulu time is a 24 hour clock from which I have to subtract either 500(eastern), 600(central), 700(mountain), or 800(pacific) from to find out the appropriate time for the zone. I have it started but there's an error saying that 'subscript requires array or pointer type'. Can anyone tell me what that means and how I can fix it please? here's the code:
#include<iostream.h>


int main()
{
int time=0;
cin>>time;
int zone=0;
cin>>zone;
for(int x=0; x<900; x++)
{
switch(zone[x])
{
case 'E': zone=500;
break;
case 'C': zone=600;
break;
case 'M': zone=700;
break;
case 'P': zone=800;
break;
default:
break;
}
}






return 0;
}
Sirena400 is offline   0 Reply With Quote
Old Feb 16, 2005, 11:56 AM   #2
jsw
Moderator emeritus
 
jsw's Avatar
 
Join Date: Mar 2004
Location: Andover, MA
Not sure exactly what your logic is trying to do there, but the problem is that you're switching on zone[x], and, since zone is an int, not an array, the "zone[x]" bit makes no sense to the compiler.
__________________
You'll be the one moaning for me to give you some. - THC(taken out of context)
jsw is offline   0 Reply With Quote
Old Feb 16, 2005, 12:00 PM   #3
jsw
Moderator emeritus
 
jsw's Avatar
 
Join Date: Mar 2004
Location: Andover, MA
I assume you want something more like the following (no results output in the code below, just a start...):

Code:
int main()
{
  int  time = 0;
  char zone = '';

  cin >> time;
  cin >> zone;

  switch (zone)
  {
    case 'E': time -= 500; break;
    case 'C': time -= 600; break;
    case 'M': time -= 700; break;
    case 'P': time -= 800; break;
    default:               break;
  }
}
__________________
You'll be the one moaning for me to give you some. - THC(taken out of context)
jsw is offline   0 Reply With Quote
Old Feb 16, 2005, 03:42 PM   #4
tutubibi
macrumors 6502a
 
tutubibi's Avatar
 
Join Date: Sep 2003
Location: localhost
Think like a machine

As an advice for the next time, try walking thru your program. It's good practice for starters in programming.

Try to execute (in your head) program step by step and you will ussualy find what's wrong pretty fast.
tutubibi is offline   0 Reply With Quote
Old Feb 17, 2005, 11:26 AM   #5
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
tutubibi- I'll try to do that, but it's hard because I really don't know exactly what I'm doing. I'm just kinda learning through trial and error and from what you guys suggest to me ^^;

jsw- Gah, I'm sorry. I should explain better what the heck I'm trying to do. Right now I just was trying to set up the values for E, C, M, and P so that when I type those letters in, they have to right value that I can subtract from the zulu time which I need to do in order to get the right time. And that sample code is similar to what I want. I took out the [x] and that turned out to be the problem so it's fixed but for some reason to values for E, C, M, and P don't seem to be right I think. When I input 1745 and E on the next line I should get 1245, but I get 0. Is there something wrong with the way I set up the values for E, C, M, and P?
#include<iostream.h>


int main()
{
int time=0;
int zone=0;
cin>>time;
cin>>zone;
for(int x=0; x<900; x++)
{
switch(zone)
{
case 'E': zone=500;
break;
case 'C': zone=600;
break;
case 'M': zone=700;
break;
case 'P': zone=800;
break;
default:
break;
}
}
int total=0;
total=(time-zone);
cout<<"The time is"<<zone<<".";




return 0;
}
Sirena400 is offline   0 Reply With Quote
Old Feb 17, 2005, 12:30 PM   #6
tutubibi
macrumors 6502a
 
tutubibi's Avatar
 
Join Date: Sep 2003
Location: localhost
First, you don't need that for loop.
Think about that, why would you do same thing 900 times? There is no loop required, you receive some values, make a decision and print results.

So, remove that for loop. That will fix part of zone "overload".
To print both values (org time and local time), just look at the jsw's code above and declare something like "int offset" and assign offsets (-500, -600 ... ) to offset (instead of time in switch statement). Than print what you need using time and offset.
tutubibi is offline   0 Reply With Quote
Old Feb 18, 2005, 11:18 AM   #7
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
Ooooh, I see. I thought you need a for statement for a loop. But how do I assign offsets though and what are they?
Sirena400 is offline   0 Reply With Quote
Old Feb 18, 2005, 12:51 PM   #8
tutubibi
macrumors 6502a
 
tutubibi's Avatar
 
Join Date: Sep 2003
Location: localhost
Quote:
Originally Posted by Sirena400
Ooooh, I see. I thought you need a for statement for a loop. But how do I assign offsets though and what are they?
You don't need a loop at all. I am calling 'offset' variable used to store time offset from zulu time (i.e. -500). Look at jsw's code above, it works, just add print statements and conversion for am or pm and and that's it.
Something like this:

Code:
...

int main()
{
  int  time = 0;
  int   offset = 0;
  char zone = '';

  cin >> time;
  cin >> zone;

  switch (zone)
  {
    case 'E': offset = 500; break;
    case 'C': offset = 600; break;
    case 'M': offset = 700; break;
    case 'P': offset = 800; break;
    default:               break;
  }

  int localtime = time-offset;
  if( localtime < 0 )
     localtime += 24;

  if( localtime < 12 )
     cout<<"ZULU time is "<<time<<", local time is " << localtime << "AM";
  else
     cout<<"ZULU time is "<<time<<", local time is " << localtime-12 << "PM";

}
Now, localtime can be negative for some zulu values. So we add 24 to make it positive value before figuring out is it AM or PM.
tutubibi is offline   0 Reply With Quote
Old Feb 22, 2005, 07:38 PM   #9
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
Okay, everything is fixed now! Thank you very much for your help again jsw and tutubibi! You guys are the best!
Sirena400 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 11:26 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC