Click here to Skip to main content
15,564,500 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: question about CDC:Pie Pin
ForNow5-Jan-22 13:37
ForNow5-Jan-22 13:37 
QuestionSaving a memory DC to a file Pin
ForNow4-Jan-22 5:42
ForNow4-Jan-22 5:42 
AnswerRe: Saving a memory DC to a file Pin
Mircea Neacsu4-Jan-22 6:10
Mircea Neacsu4-Jan-22 6:10 
GeneralRe: Saving a memory DC to a file Pin
ForNow4-Jan-22 6:29
ForNow4-Jan-22 6:29 
AnswerRe: Saving a memory DC to a file Pin
Richard MacCutchan4-Jan-22 6:13
mveRichard MacCutchan4-Jan-22 6:13 
GeneralRe: Saving a memory DC to a file Pin
ForNow4-Jan-22 6:31
ForNow4-Jan-22 6:31 
GeneralRe: Saving a memory DC to a file Pin
Victor Nijegorodov4-Jan-22 21:28
Victor Nijegorodov4-Jan-22 21:28 
QuestionHi! Im new at coding and i wanted to ask some help. Pin
panda083-Jan-22 4:21
panda083-Jan-22 4:21 
The incomplete code bellow doesnt seem to be working as intended. I would be really gratefull to any help.
#include<iostream>
#include <fstream>
#include<cstring>
using namespace std;
const int N=20; <pre lang="C++"><pre lang="text"><pre lang="C++"></pre></pre></pre>
const char Filename[]= "Shiplist.dat";
struct Ship {
unsigned int number;
float capacity;
char name[50];

};

fstream sp;

int menu() {
int choice;
cout << "\n \t MENU";
cout << "\n 1. Add individual ships";
cout << "\n 2. Add a list of ships";
cout << "\n 3. Print the list of all ships on screen";
cout << "\n 4. Show the ships with the biggest load capacity ";
cout << "\n 5. Search ship by name";
cout << "\n 6. Add shipments";
cout << "\n 7. Cancel shipments";
cout << "\n 8. Save ship information";
cout << "\n 9. Add new ship to the list";
cout << "\n 10. Exit";
do {
cout << "\n Your choice:"; cin >> choice;

} while (choice<1 || choice>10);
return choice;
}

Ship input () {
Ship S= { 0 };
cin.ignore();
cout << "\n Enter ship number:"; cin >> S.number;
cin.ignore();
cout << "\n Enter the name of the ship:"; cin.getline(S.name,50);
cout << "\n Enter the load capacity of the ship:"; cin >> S.capacity;


return (S);
}

int enter (Ship Lib[], int n) {
int i, m;
do {
cout << "\n Enter information for how many ships(max 5 in one go):";
cin >> m;
} while(m<0 || m>5);
if (n+m < 20) {
for (i= n; i<n+m; i++) {
cout << "\n Ship " << i+1;
Lib[i]= input();
}
return (n+m);

} else cout << "\n You can't add any more ships!";

}

void Save_File (Ship Lib[], int n) {
sp.open(Filename, ios::binary | ios::out);
if (!sp) { cout << "\n Error in file! \n"; exit(1); }
sp.write((char*)Lib, sizeof(Ship) * n);
sp.close();
}

void append () {
Ship b= { 0 };
sp.open(Filename, ios::binary | ios::app);
if (!sp) { cout << "\n Error in file! \n"; exit(1); }
cout << "\n Add a new ship to the list \n";
b= input();
sp.write((char*)&b, sizeof(Ship));
sp.close();

}

void output (Ship Lib[], int n) {
int i, k=0;
cout << "\n \t List of all ships \n";
for (i=0; i<n; i++) {
cout << "\n" << i+1 << "\t" << Lib[i].number << " " << Lib[i].name << " " << Lib[i].capacity ;
k++; if(k % 5 == 0) cout << "\n\n\n\n\n\n";
}
}

int Load_File (Ship Lib[]) {
long pos; int n=0, i; Ship b;
sp.open(Filename, ios::binary | ios::in);
if (!sp) { cout << "\n File doesn't exist!! \n"; return n; }


sp.seekg(0L, ios::end);
pos= sp.tellg();
sp.close();

n= pos/ (sizeof(Ship));
sp.open(Filename, ios::binary | ios::in);
if (!sp) { cout << "\n Error in file! \n"; exit(1); }
for (i=0; i<n; i++) {
sp.read((char*)&b, sizeof(Ship));
Lib[i]=b;
}

sp.close();
return n;

}

void Ship_by_Name (Ship Lib[], int n) {
char Sname[50];
cin.ignore();
cout << "\n Ship name: ";
cin.getline(Sname, 50);
for (int i = 0; i < n; i++)
if (!strcmp(Sname, Lib[i].name)) {
cout << "\n Ship found!";
cout << "\n Number: " << Lib[i].number << "\t Name: " << Lib[i].name << "\t Load capacity: "
<< Lib[i].capacity;
break;

} else cout << "\n This ship isn't recorded!";

}

int main () {
int choice, n=0;
Ship Lib[N];
char answ= 'y';
n= Load_File(Lib);
do {
choice= menu();
switch (choice) {
case 1: input(); break;
case 2: n= enter(Lib, n); Save_File(Lib, n); break;
case 3: n= Load_File(Lib); output(Lib, n); break;

case 5: n= Load_File(Lib); Ship_by_Name(Lib, n); break;

case 8: Save_File(Lib, n); break;
case 9: do {
append(); n++;
cout << "\n One more[y/n]? " << "\n Answer: "; cin >> answ;
Save_File(Lib, n);
} while (!(answ == 'N' || answ == 'n')); break;

}

} while (choice != 10);

}
AnswerRe: Hi! Im new at coding and i wanted to ask some help. Pin
Greg Utas3-Jan-22 5:26
mveGreg Utas3-Jan-22 5:26 
AnswerRe: Hi! Im new at coding and i wanted to ask some help. Pin
Richard MacCutchan3-Jan-22 6:10
mveRichard MacCutchan3-Jan-22 6:10 
AnswerRe: Hi! Im new at coding and i wanted to ask some help. Pin
David Crow3-Jan-22 8:28
David Crow3-Jan-22 8:28 
QuestionLooking for advice/guidance on writting / painting to non Client area of window Pin
ForNow30-Dec-21 5:39
ForNow30-Dec-21 5:39 
AnswerRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
Victor Nijegorodov30-Dec-21 6:50
Victor Nijegorodov30-Dec-21 6:50 
GeneralRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
ForNow30-Dec-21 8:33
ForNow30-Dec-21 8:33 
GeneralRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
Victor Nijegorodov30-Dec-21 9:49
Victor Nijegorodov30-Dec-21 9:49 
GeneralRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
ForNow30-Dec-21 10:12
ForNow30-Dec-21 10:12 
GeneralRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
Victor Nijegorodov30-Dec-21 11:19
Victor Nijegorodov30-Dec-21 11:19 
GeneralRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
ForNow30-Dec-21 11:24
ForNow30-Dec-21 11:24 
GeneralRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
charlieg30-Dec-21 12:54
charlieg30-Dec-21 12:54 
AnswerRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
RedDk30-Dec-21 8:29
RedDk30-Dec-21 8:29 
GeneralRe: Looking for advice/guidance on writting / painting to non Client area of window Pin
ForNow30-Dec-21 8:50
ForNow30-Dec-21 8:50 
QuestionActiveX control development - old farts need apply Pin
charlieg28-Dec-21 17:08
charlieg28-Dec-21 17:08 
AnswerRe: ActiveX control development - old farts need apply Pin
Richard MacCutchan28-Dec-21 23:21
mveRichard MacCutchan28-Dec-21 23:21 
GeneralRe: ActiveX control development - old farts need apply Pin
charlieg29-Dec-21 2:37
charlieg29-Dec-21 2:37 
AnswerRe: ActiveX control development - old farts need apply Pin
charlieg29-Dec-21 17:49
charlieg29-Dec-21 17:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.