C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Is this a correct dynamic structure? Posted by Ulnarian on 3 Jun 2006 at 4:15 PM
Hey all, I'm running of a problem question in this C++ book I've picked up. The problem asks me to design a dynamic structure that will keep track of the user's car models and years.

If I understand dynamic structures correctly, they are structures that are created during the runtime of a program so as to conserve memory.

With this in mind, I've created the following, but I keep getting a compiler error down in the cin portion of the while loop (marked below in the comments). Does anyone know what the problem is? Thanks!

-----------------------------------------


#include <iostream>
#include <string>


struct s_carprogram
{
std::string make;
int year;
};

int main()
{
using namespace std;
cout << "How many cars are there: ";
int cars;
cin >> cars;
int i=0;

while (i <= cars)
{
s_carprogram carloop[i];
cout << "\nEnter the car model: ";
cin >> carloop[i].make;
cout << "\nEnter year of vehicle: ";
cin >> carloop[i].year); //expected ";" before ")" token ERROR!!!!
++i;
}
Report
Re: Is this a correct dynamic structure? Posted by stober on 3 Jun 2006 at 5:29 PM
: #include <iostream>
: #include <string>
: 
: 
: struct s_carprogram
: {
: std::string make;
: int year;
: };
: 
: int main()
: {
: using namespace std;
: cout << "How many cars are there: ";
: int cars;
: cin >> cars;
: int i=0;
: 
: while (i <= cars)
: {
: s_carprogram carloop[i];
^^^ you can't declare an array that way.  


Here is a suggested change that will probably compile and work.
: int main()
: {
: using namespace std;
: cout << "How many cars are there: ";
: int cars;
: cin >> cars;
: int i=0;
 s_carprogram* carloop = new s_carprogram[cars];
: while (i <= cars)
: {
...




Report
Re: Is this a correct dynamic structure? Posted by Ulnarian on 8 Jun 2006 at 12:26 AM
Sorry, I've been...diverted for awhile. Thanks for the help. I'm going to chew on your input awhile now (for some reason, I've the darndest time mastering pointers) :)

:
: : #include <iostream>
: : #include <string>
: : 
: : 
: : struct s_carprogram
: : {
: : std::string make;
: : int year;
: : };
: : 
: : int main()
: : {
: : using namespace std;
: : cout << "How many cars are there: ";
: : int cars;
: : cin >> cars;
: : int i=0;
: : 
: : while (i <= cars)
: : {
: : s_carprogram carloop[i];
: ^^^ you can't declare an array that way.  
: 

:
: Here is a suggested change that will probably compile and work.
:
: : int main()
: : {
: : using namespace std;
: : cout << "How many cars are there: ";
: : int cars;
: : cin >> cars;
: : int i=0;
:  s_carprogram* carloop = new s_carprogram[cars];
: : while (i <= cars)
: : {
: ...
: 

:
:
:
:

Report
Re: Is this a correct dynamic structure? Posted by kooladi on 7 Jun 2006 at 3:12 AM
: Hey all, I'm running of a problem question in this C++ book I've picked up. The problem asks me to design a dynamic structure that will keep track of the user's car models and years.
:
: If I understand dynamic structures correctly, they are structures that are created during the runtime of a program so as to conserve memory.
:
: With this in mind, I've created the following, but I keep getting a compiler error down in the cin portion of the while loop (marked below in the comments). Does anyone know what the problem is? Thanks!
:
: -----------------------------------------
:
:
: #include <iostream>
: #include <string>
:
:
: struct s_carprogram
: {
: std::string make;
: int year;
: };
:
: int main()
: {
: using namespace std;
: cout << "How many cars are there: ";
: int cars;
: cin >> cars;
: int i=0;
:
: while (i <= cars)
: {
: s_carprogram carloop[i];
: cout << "\nEnter the car model: ";
: cin >> carloop[i].make;
: cout << "\nEnter year of vehicle: ";
: cin >> carloop[i].year); //expected ";" before ")" token ERROR!!!!
: ++i;
: }
:
Remove this and it will work
Programming is a race between software engineers striving to build bigger and better idiot-proof programs,and the Universe trying to produce bigger and better idiots. So far,the Universe is winning.

Report
Re: Is this a correct dynamic structure? Posted by stephl on 7 Jun 2006 at 6:54 AM
: : Hey all, I'm running of a problem question in this C++ book I've picked up. The problem asks me to design a dynamic structure that will keep track of the user's car models and years.
: :
: : If I understand dynamic structures correctly, they are structures that are created during the runtime of a program so as to conserve memory.
: :
: : With this in mind, I've created the following, but I keep getting a compiler error down in the cin portion of the while loop (marked below in the comments). Does anyone know what the problem is? Thanks!
: :
: : -----------------------------------------
: :
: :
: : #include <iostream>
: : #include <string>
: :
: :
: : struct s_carprogram
: : {
: : std::string make;
: : int year;
: : };
: :
: : int main()
: : {
: : using namespace std;
: : cout << "How many cars are there: ";
: : int cars;
: : cin >> cars;
: : int i=0;
: :
: : while (i <= cars)
: : {
: : s_carprogram carloop[i];
: : cout << "\nEnter the car model: ";
: : cin >> carloop[i].make;
: : cout << "\nEnter year of vehicle: ";
: : cin >> carloop[i].year); //expected ";" before ")" token ERROR!!!!
: : ++i;
: : }
: :
: Remove this and it will work
: Programming is a race between software engineers striving to build bigger and better idiot-proof programs,and the Universe trying to produce bigger and better idiots. So far,the Universe is winning.
:
:
I am not sure the ')' is the only problem. As Stober wrote, you cannot define an array whose size is not known at compile time (at least in C). That's why malloc() and calloc() exist (or new).

Steph



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.