PDA

View Full Version : C++ help?




Sirena400
Feb 16, 2005, 11:49 AM
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;
}



jsw
Feb 16, 2005, 11:56 AM
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.

jsw
Feb 16, 2005, 12:00 PM
I assume you want something more like the following (no results output in the code below, just a start...):


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;
}
}

tutubibi
Feb 16, 2005, 03:42 PM
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.

Sirena400
Feb 17, 2005, 11:26 AM
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;
}

tutubibi
Feb 17, 2005, 12:30 PM
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.

Sirena400
Feb 18, 2005, 11:18 AM
Ooooh, I see. I thought you need a for statement for a loop. But how do I assign offsets though and what are they?

tutubibi
Feb 18, 2005, 12:51 PM
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:


...

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.

Sirena400
Feb 22, 2005, 07:38 PM
Okay, everything is fixed now! Thank you very much for your help again jsw and tutubibi! You guys are the best! :)