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. newbie needs help

newbie needs help

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++databasevisual-studio
7 Posts 5 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.
  • M Offline
    M Offline
    marinme
    wrote on last edited by
    #1

    Hello, I'm currently trying to learn c++ and stuck on an exercise from my book. I don't have an answer guide, nor can I find the answer online. Here is the exercise: Design a structure called car that holds the following information about an automobile: its make as a string in a character array and the year it was built as an integer. Write a program that asks the user how many cars to catalog. The program then should use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care, for it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure. A sample run should look something like the following: How many cars do you wish to catalog? 2 Car #1: Please enter the make: Hudson Hornet Please enter the year made: 1952 Car #2: Please enter the make: Kaiser Please enter the year made: 1951 Here is your collection: 1952 Hudson Hornet 1951 Kaiser And here is my code: #include using namespace std; struct car { char make[30]; int year; }; int main() { int carsToLog, i; cout << "How many cars do you wish to catalog? "; cin >> carsToLog; car * pNum = new car[carsToLog]; for (i = 0;imake,29); cout << endl << "Please enter the year made: "; cin >> pNum[i]->year; } cout << endl << "Here is your collection: " << endl; for(i=0;iyear << " " << pNum[i]->make << endl; delete[] pNum; return 0; } I am getting the following errors from VS.Net: error C2819: type 'car' does not have an overloaded member 'operator ->' error C2227: left of '->make' must point to class/struct/union. When I take the array index out of the cin lines, it works, but will only access the first index of the dynamic array.. Thanks for the help in advance!

    S 1 Reply Last reply
    0
    • M marinme

      Hello, I'm currently trying to learn c++ and stuck on an exercise from my book. I don't have an answer guide, nor can I find the answer online. Here is the exercise: Design a structure called car that holds the following information about an automobile: its make as a string in a character array and the year it was built as an integer. Write a program that asks the user how many cars to catalog. The program then should use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care, for it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure. A sample run should look something like the following: How many cars do you wish to catalog? 2 Car #1: Please enter the make: Hudson Hornet Please enter the year made: 1952 Car #2: Please enter the make: Kaiser Please enter the year made: 1951 Here is your collection: 1952 Hudson Hornet 1951 Kaiser And here is my code: #include using namespace std; struct car { char make[30]; int year; }; int main() { int carsToLog, i; cout << "How many cars do you wish to catalog? "; cin >> carsToLog; car * pNum = new car[carsToLog]; for (i = 0;imake,29); cout << endl << "Please enter the year made: "; cin >> pNum[i]->year; } cout << endl << "Here is your collection: " << endl; for(i=0;iyear << " " << pNum[i]->make << endl; delete[] pNum; return 0; } I am getting the following errors from VS.Net: error C2819: type 'car' does not have an overloaded member 'operator ->' error C2227: left of '->make' must point to class/struct/union. When I take the array index out of the cin lines, it works, but will only access the first index of the dynamic array.. Thanks for the help in advance!

      S Offline
      S Offline
      Steve S
      wrote on last edited by
      #2

      You have a pointer named pNum. That can be used with the '->' operator, and also the indexing operator '[]'. Either of pNum->make or pNum[0].make refer to the same object. When you use the index operation, you don't have a pointer any more, you have an indexed instance of the thing the pointer points to. So you can say: pNum[i].make or even &pNum[i]->make but the latter is just perverse :) Hope this helps, and welcome to the wonderful world of C++ Steve S Developer for hire

      T 1 Reply Last reply
      0
      • S Steve S

        You have a pointer named pNum. That can be used with the '->' operator, and also the indexing operator '[]'. Either of pNum->make or pNum[0].make refer to the same object. When you use the index operation, you don't have a pointer any more, you have an indexed instance of the thing the pointer points to. So you can say: pNum[i].make or even &pNum[i]->make but the latter is just perverse :) Hope this helps, and welcome to the wonderful world of C++ Steve S Developer for hire

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        Steve S wrote: &pNum[i]->make but the latter is just perverse or (pNum + i)->make ?! :-D


        TOXCCT >>> GEII power

        H 1 Reply Last reply
        0
        • T toxcct

          Steve S wrote: &pNum[i]->make but the latter is just perverse or (pNum + i)->make ?! :-D


          TOXCCT >>> GEII power

          H Offline
          H Offline
          Henry miller
          wrote on last edited by
          #4

          This is a student project, not the obfiscated C++ coding contest. Any student who turns in either of those two perverse options had better have a good explination as to why they did that, and not the simple pNum[i].make! Otherwise they will lose points. I'm sure there is a good reason to use either of those forms. I can't think of one offhand though.

          T 1 Reply Last reply
          0
          • H Henry miller

            This is a student project, not the obfiscated C++ coding contest. Any student who turns in either of those two perverse options had better have a good explination as to why they did that, and not the simple pNum[i].make! Otherwise they will lose points. I'm sure there is a good reason to use either of those forms. I can't think of one offhand though.

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            yes, of course, but we're also here to share knowledges...


            TOXCCT >>> GEII power

            J 1 Reply Last reply
            0
            • T toxcct

              yes, of course, but we're also here to share knowledges...


              TOXCCT >>> GEII power

              J Offline
              J Offline
              Jeryth
              wrote on last edited by
              #6

              Sick people :wtf:X| . . . teaching pointer arithmetic to beginners.:cool: The question "Do computers think?" is the same as "Can submarines swim?"

              T 1 Reply Last reply
              0
              • J Jeryth

                Sick people :wtf:X| . . . teaching pointer arithmetic to beginners.:cool: The question "Do computers think?" is the same as "Can submarines swim?"

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                so what ?! :suss: :confused:


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

                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