473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Need Help with a Counter

I would like to search through all the records from start to finish and each time a record with onward connections is found the total count should be increased by 1.

so far i have

Expand|Select|Wrap|Line Numbers
  1. if (choice ==5) 
  2.         {
  3.             float count =0;
  4.             fstream Airline ("AirLine.text",ios::in |ios::binary); 
  5.             if(!Airline)
  6.             {
  7.                 cout << "File Could not be opened." << endl;  
  8.                 system("PAUSE");
  9.                 exit (1); 
  10.             }
  11.  
  12.             Newpassenger passenger;
  13.            while (passenger.seat_number > 0 && passenger.seat_number <= 100)
  14.             Airline.seekg((passenger.seat_number - 1 ) * sizeof(Newpassenger),ios_base::beg);
  15.             Airline.read(reinterpret_cast<char *>(& passenger), sizeof(Newpassenger));
  16.                 {
  17.                     if( passenger.onward == "y")
  18.                         count ++;
  19.                 }
  20.             return (count);
  21.  
  22.             cout << "The Number of Passengers with Onward Flights are" << count << endl;
  23.         }
  24.  
I am getting Result of comparison against a string literal is unspecified (use strncmp instead)

I tried alot of things to Search the Array and Count the amount of times a user Enter Y

Expand|Select|Wrap|Line Numbers
  1.  
  2.         if (choice == 1) // Add New Record..
  3.  
  4.  
  5.         {
  6.             fstream Airline ("AirLine.text", ios::out | ios::in |ios::binary); 
  7.             if(!Airline)
  8.             {
  9.                 cout << "File Could not be opened." << endl;  
  10.                 system("PAUSE");
  11.                 exit (1); 
  12.             }
  13.  
  14.  
  15.             cout << "Enter Seat Number to Write"
  16.             <<"(1 to 100, 0 to end )?";
  17.  
  18.  
  19.             Newpassenger passenger;
  20.             cin >> passenger.seat_number;
  21.  
  22.  
  23.             while (passenger.seat_number > 0 && passenger.seat_number <=100)
  24.             {
  25.                 cout << "Enter passengers Name:";
  26.                 cin >> passenger.name;
  27.  
  28.                 cout << "Do you have an Onward Flight: (Y/N)";
  29.                 cin >> passenger.onward;
  30.  
  31.  
  32.  
  33.                 Airline.seekp((passenger.seat_number - 1 ) * sizeof(Newpassenger));
  34.  
  35.                 Airline.write(reinterpret_cast<const char *>(&passenger), sizeof (Newpassenger));
  36.  
  37.                 cout << "\nEnter Seat Number: ?";
  38.                 cin >> passenger.seat_number;
  39.             }
  40.             Airline.close();
  41.             cout << endl;
  42.         }         
  43.  
Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. struct Newpassenger
  4.     int seat_number;
  5.     char name[20];
  6.     char onward[10];
  7. };
Allso tried
Expand|Select|Wrap|Line Numbers
  1.  if( strcmp (onward[10],"y")==0)
  2.                         count ++;
Nov 28 '11 #1

✓ answered by weaknessforcats

It's probably right here:

Expand|Select|Wrap|Line Numbers
  1. if( passenger.onward == "y"
  2. etc...)
I don't know what the type of passenger.onward is, but if it is a
C-string, then this code compare addresses rather than value. The equivalent of this code is:

Expand|Select|Wrap|Line Numbers
  1. if( !strcmp(passenger.onward,"y")
  2.  {
  3.     equal here...
  4.  }
  5. etc...

On the other hand, if passenger.onward is a C++ string variable, then your code works provded you#include <string>.

5 1674
weaknessforcats
9,208 Expert Mod 8TB
It's probably right here:

Expand|Select|Wrap|Line Numbers
  1. if( passenger.onward == "y"
  2. etc...)
I don't know what the type of passenger.onward is, but if it is a
C-string, then this code compare addresses rather than value. The equivalent of this code is:

Expand|Select|Wrap|Line Numbers
  1. if( !strcmp(passenger.onward,"y")
  2.  {
  3.     equal here...
  4.  }
  5. etc...

On the other hand, if passenger.onward is a C++ string variable, then your code works provded you#include <string>.
Nov 28 '11 #2
this is my strut
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<sstream> 
  3. #include<fstream> 
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. struct Newpassenger
  11.     int seat_number;
  12.     char name[20];
  13.     char onward[10];
  14. };
  15.  
Nov 28 '11 #3
Expand|Select|Wrap|Line Numbers
  1.  if (choice ==5) 
  2.         {
  3.  
  4.  
  5.             int  count =0;
  6.             fstream Airline ("AirLine.text",ios::in |ios::binary); 
  7.             if(!Airline)
  8.             {
  9.                 cout << "File Could not be opened." << endl;  
  10.                 system("PAUSE");
  11.                 exit (1); 
  12.             }
  13.  
  14.             Newpassenger passenger;
  15.            while (passenger.seat_number > 0 && passenger.seat_number <= 100)
  16.             Airline.seekg((passenger.seat_number - 1 ) * sizeof(Newpassenger),ios_base::beg);
  17.             Airline.read(reinterpret_cast<char *>(& passenger), sizeof(Newpassenger));
  18.                 {
  19.                     if( !strcmp (passenger.onward, "Y"))
  20.                        count ++;
  21.  
  22.                 }
  23.  
  24.  
  25.             cout << "The Number of Passengers with Onward Flights are" << count << endl;
  26.         }
  27.  
  28.  
its only giving me 0
Nov 28 '11 #4
weaknessforcats
9,208 Expert Mod 8TB
Have you observed that passenger.onward has the correct contents? I assume you are using your debugger. If not, add a display before the strcmp to see what's going on.
Nov 28 '11 #5
Hi, thanks for your help i was making life hard on myself so i moved the if( !strcmp (passenger.onward, "Y")) into
its all working well.. just output the count. without the need to search the text file.
Expand|Select|Wrap|Line Numbers
  1.  cout << "Do you have an Onward Flight: (Y/N)";
  2.                 cin >> passenger.onward;
  3. if( !strcmp (passenger.onward, "Y"))
  4. count ++
  5.  
if (choice ==5)
{
cout << "The Number of Passengers with Onward Flights are" << count << endl;
}

[code]
Nov 28 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marc | last post by:
Hello, This is a simple thing but somehow I can't work it :(((( I have a self-submitting form displaying data from a database. To keep track which data to show I want to create a variable that...
0
by: Adam Haskell | last post by:
Ok heres the situation: We have a linked server from SQL 2000 to a foxpro dbf. In our test enviroment we had the Foxpro and SQL server on the same machine. The linked worked perfect. Now we are...
25
by: Mark | last post by:
I'm just starting out in an introductory ASP.Net course, and am trying to run a simple program but keeping getting an error. I'm running XP, have installed Internet Information Services (5.1) ,...
1
by: Arvind P Rangan | last post by:
Hi, I have created a class library which i need to use in my ASPX file. when i say: Dim mylib As TestLib = new TestLib() it gives me an error saying type required. How do we use a class...
0
by: saravanan_article | last post by:
Hi I am newbie to C#, i am using C# 2005 and DataGridView in my Application. The problem is described here I am using DataGrid and I placed some Headers like Column1,Column2,Column3.... What i...
2
by: brad.goldberg | last post by:
Hey All... I have a form that has a Before Update event of: Private Sub Form_BeforeUpdate(Cancel As Integer) If Me.NewRecord Then Me!displayedRunNumber =...
4
by: Test3456 | last post by:
Hello, i got following Job: Write a programm that shows the letters a - z. The lettes should be act in some millisecounds. Start with 100 millisecounds and every loop the millisecounds show be...
9
by: tragic54 | last post by:
Alright so i'm writing a program, and i need to count the number of times a word begins with a letter. Public Class frmPigLatin Private Sub btnTranslate_Click(ByVal sender As...
8
by: rdabane | last post by:
I'm trying to perform following type of operation from inside a python script. 1. Open an application shell (basically a tcl ) 2. Run some commands on that shell and get outputs from each command...
2
by: digituf | last post by:
<html> <body> <p> <?php $result = mysql_query("SELECT distinct room_type,room_price from room1 WHERE room_no NOT IN ( SELECT id_room_no FROM reservation1 WHERE datein >='$datein' AND dateout...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.