• 12-17-2009
Cursius
Hi again. i'm back with some questions that have to do with a project im working on for the college.
This is gonna be a bit big and if there is any variable that u dont understand its use then ask me please.
So this time we have to do the following :
First i have to insert into a struct the details of every flight for 10 flights.Those are :
The "flights code" ( example : Flight OA123)
The "from" ( wich city the airplane takes off)
The "to"(wich city the airplane lands)
The "date" (date of take off)
The "time" ( time of take off)
The "price" ( the price of the ticket for each one)
The " sits" ( that each plane has for passengers)

and then
the programm should perform 2 operations.

1)Find flights.
This means that the user inputs a a departure city, a land city, and a date and searches for a flight that meets theese criteria.
If there is no flight matching the criteria it will not terminate but instead it will give the user the opportunity to search again or exit.ofcourse the results will be displayed on screen!

2)It will be able for the user to get sits.
By this i mean that the user can get a ticket for a specific flight like this:
The user inputs a flight code. If there is a flight with that code the user can buy a ticket.So if a flight has 150 sits, only 150 tickets can be bought/sold.If a flight has 120 sits then only 120 tickets can be bought/sold.If the amount that the user is trying to "buy" exceeds the amount of available sits left on the specific then the user cannot buy those sits and will get a message for the amount of sits left.If he wants 3 but there are only to then smthing like " There are only 2 more sits available" will be displayed.

the code i have so far is this :
Code:

```#include <stdio.h> #include <string> int i,opt;   struct info {         char from[25],to[25],code[6];         int date,time,price,sits; }; struct info flight[10]; void insert(),find(),sits(); void insert() //inserts values {                 flight[0].code="AO000";  flight[1].code="AO001";  flight[2].code="AO002";  flight[3].code="AO003";  flight[4].code="AO004";  flight[5].code="AO005";  flight[6].code="AO006";  flight[7].code="AO007";  flight[8].code="AO008";  flight[9].code="AO009";             flight[0].from="Athens";  flight[1].from="Thessaloniki";  flight[2].from="Larisa";  flight[3].from="Patra";  flight[4].from="Trikala";  flight[5].from="Xania";  flight[6].from="Agio Or;os";  flight[7].from="Lamia";  flight[8].from="Larnaka";  flight[9].from="Aidonia";                 flight[0].to="Patra";  flight[1].to="Trikala";  flight[2].to="Xania";  flight[3].to="Agio Oros";  flight[4].to="Aidonia";  flight[5].to="Lamia";  flight[6].to="Athens";  flight[7].to="Larnaka";  flight[8].to="Patra";  flight[9].to="Thessaloniki";                 flight[0].date=0801;  flight[1].date=0301;  flight[2].date=0403;  flight[3].date=1205;  flight[4].date=1611;  flight[5].date=2109;  flight[6].date=0709;  flight[7].date=1104;  flight[8].date=2708;  flight[9].date=2202;                 flight[0].time=1249;  flight[1].time=2210;  flight[2].time=2015;  flight[3].time=1620;  flight[4].time=0930;  flight[5].time=0730;  flight[6].time=2400;  flight[7].time=1145;  flight[8].time=1425;  flight[9].time=1945;                 flight[0].price=50;  flight[1].price=39;  flight[2].price=42;  flight[3].price=49;  flight[4].price=60;  flight[5].price=80;  flight[6].price=30;  flight[7].price=20;  flight[8].price=66;  flight[9].price=45;                 flight[0].sits=150;  flight[1].sits=100;  flight[2].sits=130;  flight[3].sits=170;  flight[4].sits=158;  flight[5].sits=134;  flight[6].sits=160;  flight[7].sits=120;  flight[8].sits=140;  flight[9].sits=150;         } void find()/* searches for a specific flight*/ {         char from[20],to[20];         int i,date;         printf("A\n Anazitisi Priseon : ");         printf("\n Parakalo eisagete poli anaxorisis : ");         scanf("%s",from);         printf("\n Parakalo eisagete poli afiksis");         scanf("%s",to);         printf("\n Parakalo eisagete imerominia");         scanf("%d",date);         printf("\n Oi akolouthes ptiseis brethikan : ");           for (i=0;i<10;i++)                             {                                     if ((!strcmp(flight[i].from,from))&&(!strcmp(flight[i].to,to))&&(flight[i].date==date))                                             {                                                     printf("\n Ptisis: %s ,Wra: %d , Timi: %d  , Thesis : %d",flight[i].code,flight[i].time,flight[i].price,flight[i].sits);                                             }                             }                     }  void sits() /* user gets tickets*/ {         char kod[6]; //kod  = code of a flight         int thesis,date;  //thesis = sits a user trys to buy         printf("\n Kratisi theseon : ");         printf("\n Parakalo epilexte ptisi : "); // user inputs a flight code         scanf("%s",kod);                 printf("\n Thesis pou thelete na kratisete : ");  /*user inputs amount of sits to buy*/         scanf("%d",&thesis);         for (i=0;i<10;i++)         {                 if (strcmp(flight[i].code,kod)==0)                 {                         if((flight[i].sits-thesis)>=0)                         {                                 flight[i].sits=flight[i].sits-thesis;                                 printf("\n Oi thesis kratithikan. Kostos : %d",flight[i].price*thesis);                         }                 }         } }                                                                                                                   int main()                   {                           printf("\n 1. Anazitisi Ptisis :");                           printf("\n 2. Kratisi theseon :");                           printf("\n 3. Eksodos :");                           printf("\n Parakalo doste tin epilogi sas : ");                           scanf("%d",&opt);                                                     insert();                                                     if ( opt==1)                           {                                   find();                           }                           if (opt==2)                           {                                   sits();                           }                           if (opt==3) //opt 3 exits the programm                           {                                   printf("Eksodos programmatos");                           }                   }```

i cant compile it because i get an error with the insert of values into the struct, but i cant understand why :S
From line 16-25 i get " ISO c++ forbid the ssigment of arrays"
From 26-45 i get" incompatible types of assigment of 'const char [number]' to 'char[25]'

if you can help me plz dont be late because i have to deliver this one 18/12 until midnight at least and i got like less than 20 hours. plus i need to sleep cause its almost 5 in the morning here :S

~ Cursy
• 12-18-2009
laserlight
Quote:

Originally Posted by Cursius
i cant compile it because i get an error with the insert of values into the struct, but i cant understand why :S
From line 16-25 i get " ISO c++ forbid the ssigment of arrays"
From 26-45 i get" incompatible types of assigment of 'const char [number]' to 'char[25]'

The reason is that string literals are arrays of const char. Consider the code member variable of your info struct. It is an array of 6 chars. Therefore, this involves an assignment of one array to another, which is not allowed:
Code:

`flight[0].code="AO000";`
A simple solution is to use std::string instead of C-style strings, i.e., change the type of the from, to and code member variables to std::string. Once you do that, you will need to modify the input/output function calls, e.g., #include <iostream> and write:
Code:

`std::cin >> from;`
instead of using scanf() (which is unsafe when used in the way that you used it since you failed to account for the maximum length of the string).

By the way, you should avoid global variables. Your i, opt and flight variables should the local variables. opt and flight should be local to the global main function, and i should be local to each loop where it is used. You can pass flight as an argument to the functions that need it: an array decays to a pointer to its first element when passed as an argument.

It would also be good if you indented your code more consistently.

Quote:

Originally Posted by pelicanpie
Fixed.

It is not fixed, and you should not be providing direct homework solutions before Cursius has had the chance to improve his/her program with more descriptive help.
• 12-18-2009
laserlight
Quote:

Originally Posted by pelicanpie
but it compiles with no errors!!!

That does not mean that it is correct. You might get some useful warnings if you increased the warning level that you compile at.

EDIT:
Actually, the MinGW port of g++ 3.4.5 gives me some useful warnings even at the default warning level, though it takes /W3 to get them on MSVC9.
• 12-18-2009
jeffcobb
Ah yes...
Quote:

Originally Posted by pelicanpie
but it compiles with no errors!!!

Behold the devilish fun of a logic error versus a syntax error...... with a syntax error your program (or compiler) won't do what you told it to whereas with a logic error the program does exactly what you told it...which is worng* ^___^

*Shades of the movie "West World"...

Peace
• 12-19-2009
Cursius
ok i've re-wrote the parts that needed fixing using strcpy and by re-assigning the int values with an other way :)
@pelicanpie thank you really for the code but im rly sorry, i did not use your code cause i like to write my own and not to just get it done :) dont get this in the wrong way tho, rly thanks for your effort!

@laser why should i avoid global variables??
it seems easyer to have them global so i dont have to pass them into functions or anything.
• 12-19-2009
laserlight
Quote:

Originally Posted by Cursius
@laser why should i avoid global variables??
it seems easyer to have them global so i dont have to pass them into functions or anything.

The use of global variables tends to make your functions less reusable, or at least much more awkward to reuse. Consider what happens if you want to use function with the same type but a different name from the global variable, but the function only works on the global variable, even though it conceptually it could work on any variable of the same type.

The indiscriminate use of global variables can make your program difficult to debug and maintain since it may be far from obvious that the state of the global variable is changed by an innocent function call that appears to have nothing to do with the global variable.
• 12-19-2009
Cursius
kk then:)
im not very familiar with how to pass variables into funtions and especially arrays into functions that why i use mostly global variables :) for now at least.
but ill keep what you sayed in mind,im new to programming and C so i guess ill understand theese things better the more i get my hands on them......
• 12-19-2009
cyberfish
Quote:

@pelicanpie thank you really for the code but im rly sorry, i did not use your code cause i like to write my own and not to just get it done :) dont get this in the wrong way tho, rly thanks for your effort!
Not many new members we get here are really trying to learn like you are. Most of them just want to pass the course and graduate with a piece of paper and nothing in the brain. Keep it up :).

pelicanpie is the new resident devil here, posting (sometimes correct) solutions to all homework problems and encourage cheating and not learning. To show off (pro homework doing skillz?) or to lower the average competency of future graduates... I don't know. But most of us aren't like that and are willing to actually help each other learn.

Good to see people like you coming :).