Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array Help Please (user defined size)

Array Help Please (user defined size)

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelptutorial
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    planetx22
    wrote on last edited by
    #1

    I have having a bit of trouble, i need to write a program that converts fahrenheit to celsius (i can do the function for this part) but it needs to start with a loop that first prompts the user to "Enter Temperature:" and then stores the value entered into the array which is supposed to be no more than 50. I am having issues with how to input data into the array starting at 0 and keep going up 1 (all I can do is input data directly to myarray[50], but I want the user to input data and it be entered into the array like enter data one > put in array[0] enter data 2 > put in array[1] The program also has to ask the user if they want to keep entering data by pressing 1, or stop by pressing 0. The program then has to display how many arrays there are of temperatures (not the value of them, just how many there are). Please help!! This is what I have so far, any help greatly appreciated. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; // call this function to convert celsius to fahrenheit (function name is convert) double convert(double celsius){ double fahrenheit = ((9.0/5.0) * celsius) + 32; return fahrenheit; }; int _tmain(int argc, _TCHAR* argv[]) { double myarray[50]; int flag; flag =1; while (flag==1) { // loop cout << "Enter Temperature Reading:"; cin >>myarray[50] ;cout << " Enter 1 to input more values, 0 to stop"; cin >>flag; } return 0; }

    B D 2 Replies Last reply
    0
    • P planetx22

      I have having a bit of trouble, i need to write a program that converts fahrenheit to celsius (i can do the function for this part) but it needs to start with a loop that first prompts the user to "Enter Temperature:" and then stores the value entered into the array which is supposed to be no more than 50. I am having issues with how to input data into the array starting at 0 and keep going up 1 (all I can do is input data directly to myarray[50], but I want the user to input data and it be entered into the array like enter data one > put in array[0] enter data 2 > put in array[1] The program also has to ask the user if they want to keep entering data by pressing 1, or stop by pressing 0. The program then has to display how many arrays there are of temperatures (not the value of them, just how many there are). Please help!! This is what I have so far, any help greatly appreciated. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; // call this function to convert celsius to fahrenheit (function name is convert) double convert(double celsius){ double fahrenheit = ((9.0/5.0) * celsius) + 32; return fahrenheit; }; int _tmain(int argc, _TCHAR* argv[]) { double myarray[50]; int flag; flag =1; while (flag==1) { // loop cout << "Enter Temperature Reading:"; cin >>myarray[50] ;cout << " Enter 1 to input more values, 0 to stop"; cin >>flag; } return 0; }

      B Offline
      B Offline
      Bob Flynn
      wrote on last edited by
      #2

      You are off to a good start. I see what you need, but there is a practice around here not to do homework for students. So you tell me what it does so far, and more importantly what still needs to be done.

      P 1 Reply Last reply
      0
      • P planetx22

        I have having a bit of trouble, i need to write a program that converts fahrenheit to celsius (i can do the function for this part) but it needs to start with a loop that first prompts the user to "Enter Temperature:" and then stores the value entered into the array which is supposed to be no more than 50. I am having issues with how to input data into the array starting at 0 and keep going up 1 (all I can do is input data directly to myarray[50], but I want the user to input data and it be entered into the array like enter data one > put in array[0] enter data 2 > put in array[1] The program also has to ask the user if they want to keep entering data by pressing 1, or stop by pressing 0. The program then has to display how many arrays there are of temperatures (not the value of them, just how many there are). Please help!! This is what I have so far, any help greatly appreciated. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; // call this function to convert celsius to fahrenheit (function name is convert) double convert(double celsius){ double fahrenheit = ((9.0/5.0) * celsius) + 32; return fahrenheit; }; int _tmain(int argc, _TCHAR* argv[]) { double myarray[50]; int flag; flag =1; while (flag==1) { // loop cout << "Enter Temperature Reading:"; cin >>myarray[50] ;cout << " Enter 1 to input more values, 0 to stop"; cin >>flag; } return 0; }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        planetx22 wrote:

        cin >>myarray[50]

        You're going to the same spot each time. Use a variable here.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        P 1 Reply Last reply
        0
        • B Bob Flynn

          You are off to a good start. I see what you need, but there is a practice around here not to do homework for students. So you tell me what it does so far, and more importantly what still needs to be done.

          P Offline
          P Offline
          planetx22
          wrote on last edited by
          #4

          No problem, I understand and I hope I can learn through my errors. So far my program I think stores a temporary function at the start that converts any value of number entered into a double fahrenheit and returns it to another double celsius. I stored that there just for formula's sake, and haven't implemented it or really know how to use it yet. Then i think in this part I have correctly made an array of 50 doubles called myarray. Then i have made an int value flag that is in place of if the user puts a 1 or 0 to continue/stop the program. I want to be able to make an array that is assumed to be less than 50 values, and then the user to keep entering values until done, but I think I have it setup wrong is it just storing the values inputted into myarray[50] only? Do I have to seperatly do myarray[0], myarray[1], etc each time or is there some other loop i can make that can tell the program to keep entering values starting from [0] until when the user wants to stop. Then it should print out how many arrays there are rather than values in them. I just can't figure out how to stop the array and have the values only how many the user inputed, not all 50. Any help greatly appreciated thanks. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; // call this function to convert celsius to fahrenheit (function name is convert) double convert(double celsius){ double fahrenheit = ((9.0/5.0) * celsius) + 32; return fahrenheit; }; int _tmain(int argc, _TCHAR* argv[]) { double myarray[50]; int flag; flag =1; while (flag==1) { // loop cout << "Enter Temperature Reading:"; cin >>myarray[50] ;cout << " Enter 1 to input more values, 0 to stop"; cin >>flag; } return 0; }

          1 Reply Last reply
          0
          • D David Crow

            planetx22 wrote:

            cin >>myarray[50]

            You're going to the same spot each time. Use a variable here.


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            P Offline
            P Offline
            planetx22
            wrote on last edited by
            #5

            if i make another variable, say i.. and define it like int i i=0 (not sure if this is right) for i=0, i<50, i++ { // cout "Enter Temperatures"; cin >> myarray[i] does that work better?

            D 1 Reply Last reply
            0
            • P planetx22

              if i make another variable, say i.. and define it like int i i=0 (not sure if this is right) for i=0, i<50, i++ { // cout "Enter Temperatures"; cin >> myarray[i] does that work better?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              planetx22 wrote:

              does that work better?

              Probably, but you should surround your code snippet with <pre> tags and replace angle brackets with their non-translated version (see the buttons above the smileys).


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              P 1 Reply Last reply
              0
              • D David Crow

                planetx22 wrote:

                does that work better?

                Probably, but you should surround your code snippet with <pre> tags and replace angle brackets with their non-translated version (see the buttons above the smileys).


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                "Judge not by the eye but by the heart." - Native American Proverb

                P Offline
                P Offline
                planetx22
                wrote on last edited by
                #7

                ok thank you. If i do use this and user myarray[i] as the user input, will this put data into 0-49 of the array , or until the user wants to stop?

                D 1 Reply Last reply
                0
                • P planetx22

                  ok thank you. If i do use this and user myarray[i] as the user input, will this put data into 0-49 of the array , or until the user wants to stop?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  planetx22 wrote:

                  ...will this put data into 0-49 of the array...

                  Yes. Why not use one of the STL containers (e.g., vector, list) instead? That way, just enough room is allocated.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  P 1 Reply Last reply
                  0
                  • D David Crow

                    planetx22 wrote:

                    ...will this put data into 0-49 of the array...

                    Yes. Why not use one of the STL containers (e.g., vector, list) instead? That way, just enough room is allocated.


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                    "Judge not by the eye but by the heart." - Native American Proverb

                    P Offline
                    P Offline
                    planetx22
                    wrote on last edited by
                    #9

                    haven't covered that much so we didn't have it mentioned as part of the project. I will look into them and post if I have any problems, thanks.

                    D 1 Reply Last reply
                    0
                    • P planetx22

                      haven't covered that much so we didn't have it mentioned as part of the project. I will look into them and post if I have any problems, thanks.

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      planetx22 wrote:

                      haven't covered that much so we didn't have it mentioned as part of the project.

                      That's fine. I found it better to know how to do it the "hard" way first. Then when I started using MFC (and some of STL), I understood it a lot better.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                      "Judge not by the eye but by the heart." - Native American Proverb

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups