0

How can I create an array that is sized depending on the user input and loops the insert of the values until it reaches the end of the array ?

here is what i tried and while debugging i get the memory adress as the output

#include "stdafx.h"
#include <iostream>

using namespace std; // namespace à inclure pour utiliser cou, cin , etc...


int main()
{

    unsigned int note;
    unsigned int nombreDeNotesASaisir = 0; // initialisation à 0 obligatoire sinon pas d'instanciation de tableau , unisgned = pas de nombres négatifs

    //demande à l'utilisateur le nombre de notes à saisir
    cout << "Entrez le nombre de notes à saisir" << "\n";
    cin >> nombreDeNotesASaisir;

    int* tableauDeNotes = new int[nombreDeNotesASaisir] {}; //instanciation du tableau de notes

    // demande à l'utilisateur de saisir les notes (valeur entre 0 et 20)
    cout << "Saisissez les notes que vous souhaitez entrer dans le tableau" << "\n";

    for (int i = 0; i < nombreDeNotesASaisir; i++)
    {
        cin >> note;
        tableauDeNotes[i] = note;
    }

    cout << tableauDeNotes;

    //Gère les débordements de capacité

    //calcule la moyenne des notes

    //Affiche la moyenne de toutes les notes



    return 0;
}

what am I doing wrong ?

En_Tech_Siast
  • 83
  • 1
  • 12
  • Probably you are doing nothing wrong, print the array after taking input and see if that is incorrect. Use this code to print `for (int i = 0; i < nombreDeNotesASaisir; i++) { cout << tableauDeNotes[i] << " "; } cout << endl;` – Nishant Nov 28 '16 at 20:14
  • 1
    algorithm seems ok - printing is wrong. Print elements in loop – Jacek Cz Nov 28 '16 at 20:14
  • 4
    `std::vector` is your friend here. – NathanOliver Nov 28 '16 at 20:14
  • @NathanOliver In general (actual industry use or later use in less trivial programs), yes. But since this user is presumably still trying to learn the basics of C++, they should definitely learn how to do things manually / "the hard way" first. – Harper Nov 28 '16 at 20:31
  • thank you for the help, it worked, but how would you use the std::vector ? I'm trying different methods to see which one is the most effective. – En_Tech_Siast Nov 28 '16 at 20:35
  • See [here](http://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers) for information about how to use both `new[]` & `delete[]` (to allocate and deallocate a dynamic array) and `std::vector` (dynamic, resizeable array class that handles `new[]` and `delete[]` for you). – Justin Time - Reinstate Monica Nov 28 '16 at 22:12
  • Disagree most strenuously on that, @Harper . People should be learning good logic and structure first. Low-level implementation details can wait for after the learner has demonstrated they can make useful programs. – user4581301 Nov 28 '16 at 23:41
  • I agree on logic and structure, but I think that can be accomplished by writing small programs with simple data structures that are entirely implemented by hand. Then work up to the point where you've basically written your own implementation of things like the `vector` class, and then use the STL once you know how it works. My rule of thumb has always been "Don't use something you couldn't write (or at least understand how it's written)", and it's stood me in pretty good stead. – Harper Nov 29 '16 at 00:21

0 Answers0