Memoria dinámica - Parecido a Twitter

Iniciado por jonanderdiez, 25 Enero 2016, 16:33 PM

0 Miembros y 1 Visitante están viendo este tema.

jonanderdiez

Buenas gente, necesito ayuda para generalizar este programa. Es una especie de twitter muy simple, y estoy empezando, pero necesito saber cómo hacer una reserva dinámica de memoria, en vez de que se guarden de antemano 20 usuarios y 1000 para mensajes. (Usando punteros, vector....)

Ayuda porfavor!!!!



#include "stdafx.h" // file with pre-compiled headers
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <sstream>

using namespace std;

// This struct will contain all the information regarding a message
struct Message {
  string user_name;
  string content;
};

// Messages list
const int MAX_NUMBER_OF_MESSAGES = 1000;
Message g_messages[MAX_NUMBER_OF_MESSAGES];
int g_messages_count = 0;

// Users database
const int MAX_NUMBER_OF_USERS = 20;
string g_users[MAX_NUMBER_OF_USERS];
int g_users_count = 0;

{
  string input;
  getline(cin, input);
  stringstream my_stream(input);
  if (my_stream >> a_number) return true;
  return false;
}

// Transforms the given string to lowercase
void string_to_lower(string& str)
{
  transform(str.begin(), str.end(), str.begin(), ::tolower);
}

// Checks if an user name already exists
bool exist_username(const string& user_name)
{
  for (int ii = 0; ii < g_users_count; ++ii) {
    if (g_users[ii] == user_name) return true;
  }
  return false;
}

// Adds an user name to the list
// Fails if no more users can be added
bool add_username(const string& user_name)
{
  if (exist_username(user_name)) return true;

  if (g_users_count < MAX_NUMBER_OF_USERS) {
    g_users[g_users_count++] = user_name;
    return true;
  }

  return false;
}

// Adds a message to the list
// Fails if no more users can be added
bool add_message(const string& user_name, const string& content)
{
  if (g_messages_count < MAX_NUMBER_OF_MESSAGES) {
    Message message;
    message.user_name = user_name;
    message.content = content;

    g_messages[g_messages_count++] = message;

    return true;
  }
  return false;
}

int main(int argc, char* argv[])
{
  while (true) {
    cout << "================" << endl;
    cout << "= BAAER client =" << endl;
    cout << "================" << endl << endl;
    cout << "1. Baa" << endl;
    cout << "2. Print timeline" << endl;
    cout << "3. Quit" << endl;
    cout << "Choose an option: ";

    int op;
    if (read_integer(op) && op >= 1 && op <= 3) {
      if (op == 1) { // baa (write a message)
        cout << "Username: ";
        string user_name;
        getline(cin, user_name);
        string_to_lower(user_name);

        if (add_username(user_name)) {
          cout << "Message: ";
          string content;
          getline(cin, content);

          if (!add_message(user_name, content)) {
            cerr << "ERROR: Maximum number of messages reached!" << endl;
          }
        } else {
          cerr << "ERROR: Maximum number of users reached!" << endl;
        }
      } else if (op == 2) { // print timeline
        cout << "Timeline's username (empty for all)? ";
        string user_name;
        getline(cin, user_name); // use getline to allow empty strings

        if (!user_name.empty() && !exist_username(user_name)) {
          cerr << "ERROR: User doesn't exists" << endl;
        } else {
          cout << endl << "----------------" << endl;
          if (user_name.empty()) { // all
            for (int ii = 0; ii < g_messages_count; ++ii) {
              cout << "[@" << g_messages[ii].user_name << "]: " << g_messages[ii].content << endl;
            }
          } else { // specific user
            string_to_lower(user_name);
            for (int ii = 0; ii < g_messages_count; ++ii) {
              if (g_messages[ii].user_name == user_name) {
                cout << "[@" << user_name << "]: " << g_messages[ii].content << endl;
              }
            }
          }
          cout << "----------------" << endl;
        }
      } else break;
    } else {
      cerr << "ERROR: Invalid option" << endl;
    }
    cout << endl;
  }

  return 0;
}[code=cpp]


[/code]

ivancea96

Un ejemplo

Código (cpp) [Seleccionar]
int messageCount = 50;
Message *messages = new Message[mssageCount];

for(int i=0; i<messageCount; i++)
    ...

delete[] messages;