Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.61 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Type casting error in Visual C  
  2. #include "stdafx.h"
  3. #include "fstream"
  4. #include "conio.h"
  5. #include "string"
  6. #include "iostream"
  7.  
  8. using namespace std;
  9.  
  10. int count = 0;
  11.  
  12. class student
  13. {
  14.     char name[50], grade;
  15.     float mks;
  16. public:
  17.     student()
  18.     {
  19.         mks = 0;
  20.     }
  21.     void getdata();
  22.     void putdata();
  23.     void moddata();
  24. };
  25. void student :: getdata()
  26. {
  27.     cout << "Enter data for record number: " << "(++count)" << ": ";
  28.     cout << "nEnter name of the student: ";
  29.     cin >> name;
  30.     cout << "nEnter marks: ";
  31.     cin >> mks;
  32.     cout << "nEnter grade: ";
  33.     cin >> grade;
  34.     _getch();
  35. }
  36. void student :: putdata()
  37. {
  38.     cout << "nDisplaying data of required record: ";
  39.     cout << "nName: " << name << "nMarks: " << mks << "nGrade: " << grade;
  40.     _getch();
  41. }
  42. void student :: moddata()
  43. {
  44.     cout << "Enter correct details:-" << endl;
  45.     cout << "Enter name: ";
  46.     cin >> name;
  47.     cout << "nEnter marks: ";
  48.     cin >> mks;
  49.     cout << "nEnter grade: ";
  50.     cin >> grade;
  51.     _getch();
  52. }
  53.  
  54. int _tmain(int argc, _TCHAR* argv[])
  55. {
  56.     fstream stu ("stud.txt", ios::in | ios::out);
  57.     student s1;
  58.     char ans = 'y';
  59.     int ch, offset, mrec;
  60.     do
  61.     {
  62.         cout << "nMAIN MENU:" << endl;
  63.         cout << "1. Add Record" << endl;
  64.         cout << "2. Modify Record" << endl;
  65.         cout << "3. Display Record" << endl;
  66.         cout << "4. Exit" << endl;
  67.         cout << "Enter your choice (1-4): ";
  68.         cin >> ch;
  69.         switch (ch)
  70.         {
  71.         case 1: s1.getdata();
  72.                 stu.write ( (char*) &s1, sizeof (student) );
  73.                 break;
  74.         case 2: if (!count)
  75.                 {
  76.                     cout << "No record added yet. Type option number first.";
  77.                     _getch();
  78.                     break;
  79.                 }
  80.                 cout << "Enter the record number to be modified: ";
  81.                 cin >> mrec;
  82.                 if (mrec > count)
  83.                 {
  84.                     cout << "Error. Only " << count << " records have been added.";
  85.                     _getch();
  86.                     break;
  87.                 }
  88.                 offset = (mrec - 1)* sizeof (student);
  89.                 stu.seekg (offset);
  90.                 stu.read ( (char*) &s1, sizeof (student) );    //C2440
  91.                 s1.putdata();
  92.                 cout << "Do you want to modify your record (y/n): ";
  93.                 cin >> ans;
  94.                 if (ans == 'y' || ans == 'Y')
  95.                 {
  96.                     s1.moddata();
  97.                     stu.seekg(offset);
  98.                     stu.write ( (char)* &s1, sizeof (student) );
  99.                     break;
  100.                 }
  101.                 break;
  102.         case 3: if (!count)
  103.                 {
  104.                     cout << "No record added yet. Type option number first.";
  105.                     _getch();
  106.                     break;
  107.                 }
  108.                 cout << "Enter record number to be displayed: ";
  109.                 cin >> mrec;
  110.                 if (mrec > count)
  111.                 {
  112.                     cout << "Error. Only " << count << " records have been added.";
  113.                     _getch();
  114.                     break;
  115.                 }
  116.                 offset = (mrec - 1)*, sizeof (student);
  117.                 stu.seekg (offset);
  118.                 stu.read ( (char)* &s1, sizeof (student) );    //C2440
  119.                 s1.putdata();
  120.                 break;
  121.         case 4: break;
  122.         default: cout << "Wrong choice.";
  123.         }
  124.     } while (ch != 4);
  125.     _getch();
  126.     return 0;
  127. }
  128.        
  129. C2440: 'type cast' : cannot convert from 'student' to 'char' 1>       No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called