Thread: the compiler goes crazy with this code from some reason...

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    4

    the compiler goes crazy with this code from some reason...

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <allocators>
    using namespace std;
    
    typedef struct {
        int first;
        int seconf;
    }
    hour;
    
    typedef struct {
        int day;
        int munth;
        int year;
    }
    date;
    
    
    
    typedef struct {
        date date1;
        hour hour1;
        char descrube[100];
    }
    meet;
    
    meet read() {
        meet read1;
        cout << "date:" << endl;
        cin >> read1.date1.day;
        cin >> read1.date1.munth;
        cin >> read1.date1.year;
        cout << "hour:" << endl;
        cin >> read1.hour1.first;
        cin >> read1.hour1.seconf;
        cout << "describe:" << endl;
        cin >> read1.descrube;
        return read1;
    }
    void print1(meet print, int num) {
        cout << "the " << num << " meeting is:" << endl;
        cout << print.date1.day << "/" << print.date1.munth << "/" << print.date1.year << endl;
        cout << print.hour1.first << ":" << print.hour1.seconf << endl;
        cout << print.descrube << endl;
    }
    meet *add(meet *a, int q) {
        a = (meet*)realloc(a, sizeof(meet) *  q);
        if (a == NULL) {
            cout << "faild to allocate..." << endl;
            exit(1);
        }
        cout << "put the new meeting:" << endl;
        a[q - 1] = read();
        return a;
    }
    meet *del(meet* d, int w) {
        int x, l,dint = 0;
        cout << "what is the meeting number u want to delite: ? " << endl;
        cin >> x;
        x -= 1;
        meet *old = d;
        for (l = 0; l < w; l++) {
            if (l == x) {
                l++;
            }
            d[dint] = old[l];
            dint++;
        }
        d = (meet*)realloc(d, sizeof(meet) * w);
        if (d == NULL) {
            cout << "faild to allocate.." << endl;
            exit(2);
        }
        return d;
    }
    
    int main()
    {
        meet *m = NULL;
        char c = 'y';
        int counter = 0, p, num1 = 1;
    
        cout << "menu:" << endl;
        cout << "enter a to add a meeting:" << endl;
        cout << "enter d to delete a meeting:" << endl;
        cout << "enter p to pring the list of meetings:" << endl;
        cout << "enter e to exit:" << endl;
    
    
    
        while (c != 'e') {
        
            
            cin >> c;
            if (c == 'a') {
                counter++;
                m = add(m, counter);
            }
            else if (c == 'd') {
                counter--;
                m = del(m, counter);
            }
            else if (c == 'p') {
                num1 = 1;
                for (p = 0; p < counter; p++) {
                    print1(m[p], num1);
                    num1++;
                }
                
            }
            else;
            
            
            cout << "menu:" << endl;
            cout << "enter a to add a meeting:" << endl;
            cout << "enter d to delete a meeting:" << endl;
            cout << "enter p to pring the list of meetings:" << endl;
            cout << "enter e to exit:" << endl;
            c = 'y';
        }
    
        
    
        
        return 0;
    }
    Edit & Run


  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    4
    after i press 'a' and put the details it gives me the menu 4 times, than i press 'a' and put details and press enter and it gives me the menu couple of times again.. in some point it get looped with no end..

  3. #3
    Guest
    Guest
    Also posted here.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Part of the problem is in your read() function.

    The user might interact with the function in the following way.

    menu:
    enter a to add a meeting:
    enter d to delete a meeting:
    enter p to pring the list of meetings:
    enter e to exit:
    a
    put the new meeting:
    date:
    20 12 2015
    hour:
    10 30
    describe:
    Morning budget strategy


    The trouble is that you actually used the wrong input function for the job. operator>> will only read the first word. The rest of the data will stay in the keyboards buffer and be read by later statements, which will put the cout stream in a bad state. You can fix this problem by calling cin.ignore() followed by cin.getline() instead.

    Note also that if realloc() should fail in acquiring space the old memory is not freed. But why isn't your class using operator new instead of *alloc()/free()? Why isn't your class using std::string?

    Why is your program filled with misspelled variable names?
    Last edited by whiteflags; 12-19-2015 at 08:18 PM.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    4
    Well.. ill use it.. but how is it sopuse to solve my multiple menu appearing problem ??

  6. #6
    Registered User
    Join Date
    Dec 2015
    Posts
    4
    Its working great ! U r a genius ! Thank u very much ! Nobody else able to help me !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C compiler accepting c++ code!!
    By suryap.kv1bbsr in forum C Programming
    Replies: 4
    Last Post: 03-22-2011, 08:35 AM
  2. Code for C Compiler using lex and yacc
    By jaadugary in forum C Programming
    Replies: 0
    Last Post: 05-10-2003, 09:11 AM
  3. Replies: 3
    Last Post: 01-19-2003, 04:08 PM
  4. Code or Compiler?
    By skyruler54 in forum C++ Programming
    Replies: 0
    Last Post: 09-08-2002, 03:20 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

Tags for this Thread