Hello, guys.

I've wrote the following code

#include <iostream>
#include <string.h>
#include <string>
#include <locale>
#include <stdio.h>
#include <stdlib.h>
#include <limits>
#include <vector>
#include <windows.h>

using namespace std;



string Numero_de_Matricula = "";
string Nome = "";
int Numero_de_Materias = 0;
int Numero_de_Creditos = 0;
vector<double> creditos (10);
vector<double> Coeficientes(10);
double Coeficiente_Total = 0;
bool repetir = false;

void ClearScreen();
void Perguntar_Matricula_e_Nome(string,string);
void Perguntar_Materias_e_Creditos();
void Perguntar_Coeficientes(int);
void Calcular_Media(vector<double>&,int);
bool Repetir(bool);

void ClearScreen()
  {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }


void Perguntar_Matricula_e_Nome(string Numero_de_Matricula, string Nome)
{
    int Dados_Corretos = 0;
    Coeficiente_Total = 0;
    cout << "Digite o seu número de matrícula." << endl;
    getline(cin,Numero_de_Matricula);
    cout << "Digite o seu nome (sem acentos)." << endl;
    getline(cin,Nome);
    cout << "Seu número de matrícula e nome são:" << endl;
    cout << Numero_de_Matricula << "\t " << Nome << endl;
    cout << "Seus dados conferem?" << endl;
    cout << "1 - Sim" << endl;
    cout << "2 - Não" << endl;
    cin >> Dados_Corretos;
    cin.sync();
    if(!cin) // or if(cin.fail())
    {
        // user didn't input a number
        cin.clear(); // reset failbit
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
        // next, request user reinput
        cout << "\nOpção inválida." << endl << endl;
        Perguntar_Matricula_e_Nome(Numero_de_Matricula, Nome);
    }
    else
    {
    switch(Dados_Corretos)
    {
    case 1:
        Perguntar_Materias_e_Creditos();
        break;
    case 2:
        cout << "\n\n" << endl;
        Perguntar_Materias_e_Creditos();
        break;
    }
    }
}

void Perguntar_Materias_e_Creditos()
{
    cout << "Digite o número de matérias" << endl;
    cin >> Numero_de_Materias;
    cin.sync();
    if(!cin) // or if(cin.fail())
    {
        // user didn't input a number
        cin.clear(); // reset failbit
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
        // next, request user reinput
        cout << "\nDigite um número." << endl << endl;
        Perguntar_Materias_e_Creditos();
    }
    else
    {
    cout << "Digite o número de créditos (horas por semana):" << endl;
    cin >> Numero_de_Creditos;
    cin.sync();
    if(!cin) // or if(cin.fail())
    {
        // user didn't input a number
        cin.clear(); // reset failbit
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
        // next, request user reinput
        cout << "\Digite um número." << endl << endl;
        Repetir(repetir);
    }
    else
    {
    }
    Perguntar_Coeficientes(Numero_de_Materias);
    }
}

void Perguntar_Coeficientes(int Numero_de_Materias)
{
    int i = 0;
    for (i = 0; i < Numero_de_Materias; i++)
    {
        cout << "\n\nDigite o coeficiente da " << i+1 << "ª " << "matéria" << endl;
        cin >> Coeficientes[i];
        cin.sync();
        if(!cin) // or if(cin.fail())
    {
        // user didn't input a number
        cin.clear(); // reset failbit
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
        // next, request user reinput
        cout << "\nDigite um número." << endl << endl;
        Perguntar_Coeficientes(Numero_de_Materias);
    }
    else
    {
        cout << "\nDigite o número de créditos dessa matéria" << endl;
        cin >> creditos[i];
        cin.sync();
        if(!cin) // or if(cin.fail())
    {
        // user didn't input a number
        cin.clear(); // reset failbit
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
        // next, request user reinput
        cout << "\nDigite um número." << endl << endl;
        Repetir(repetir);
    }
    }
}
Calcular_Media( Coeficientes, Numero_de_Creditos);
}

void Calcular_Media (vector<double>& Coeficientes, int Numero_de_Creditos)
{
    int j = 0;
    for (j = 0; j < Numero_de_Materias; j++)
    {
        Coeficiente_Total += Coeficientes[j]*creditos[j];
    }
    cout << "\n\nSeu coeficiente acumulado é:" << endl;
    cout << (Coeficiente_Total)/(Numero_de_Creditos) << endl;
    Coeficiente_Total = 0;
    Coeficientes.clear();
    creditos.clear();
    Repetir(repetir);
}

bool Repetir (bool repetir)
{
    int Sim_ou_Nao = 0;
    cout << "\n\nDeseja usar o programa novamente?(1/2)" << endl;
    cin >> Sim_ou_Nao;
    cin.sync();
    if (Sim_ou_Nao == 1)
    {
        repetir = true;
        cin.sync();
        ClearScreen();
        Perguntar_Matricula_e_Nome(Numero_de_Matricula, Nome);
    }
    else if ( Sim_ou_Nao == 2)
    {
        cout << "\nAdeus!" << endl;
    }
    else if(!cin) // or if(cin.fail())
    {
        // user didn't input a number
        cin.clear(); // reset failbit
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
        // next, request user reinput
        cout << "\nOpção inválida." << endl << endl;
        Repetir(repetir);
    }
    return 0;
}


int main()
{
    setlocale(LC_ALL, "Portuguese");
    Perguntar_Matricula_e_Nome(Numero_de_Matricula, Nome);
    return 0;
}

and it works just fine. However, I'd like for my vectors to have the size of the user-inputed variable Numero_de_Materias . I tried using vector<double> Coeficientes(Numero_de_Materias) but it made my for-loop crash.

How should I declare my vector in order for it to work?

Thank you for your time.

Petcheco.

Part of your problem is that you are using the same name for the global Numero_de_Materias as well as the argument name for a number of functions. DON'T DO THAT! use different names. This is why I try to indicate that a variable is global with a leading g_Name or static with s_Name. That will keep things clean. If you want to continue this discussion via private mail yo puedo hablar espanol con fluencia.

That said, I haven't analyized the code enough to know why it is failing for you as yet.

I don't understand what you mean by 'argument name'. Why can't I use a global variable as a parameter for a function? I didn't creat any local variables named Numero_de_Materias.

Well, I appreciate the effort, but I can write and understand English without any problems. Also, I feel I have to make the distinction: the language used on my program is Portuguese, not Spanish. That said, if you still want to use PM's I wouldn't mind. Vectors always troubled me.

Let me know any others tips regarding my problem.

Thank you.

Petcheco.

(I think that even if we use PM's, we should keep on updating the thread until a solution appears so that someone else facing the same problem can fix it).

One option you have is to declare the vector as pointer,vector<double>* Coeficientes;. Now to initialize it use the new operator, after the user inputs Numero_de_Materias,Coeficientes = new vector<double>(Numero_de_Materias);. Then before you exit the program make sure to delete it, to release the memory, delete(Coeficientes);

This will allow indexed access before you populate the vector with new values. One caveat to remember, if you use push_back it will extend beyond the size you've set, not add a value to the next index to be populated.

Thank you for your answer, sir.

But in that case I would have to change the declaration of my function, wouldn't I?
Also, accessing the vector as a pointer is different than accessing a normal vector, right?

Would it be possible for you to make a small sample code showing how to properly operate with a pointer?

Again, thank you very much.

Petcheco.

Sorry I kind of jumped the gun in my first answer, it's been awhile since I worked with vectors. You can simply use the resize function:

Coeficientes.resize(Numero_de_Materias);

This way assigning values by index remains the same.

Thank you very much, sir!

You really helped me here. It worked like a charm.

All the best!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.