Solved

New to C++, how to list variables?

Posted on 2002-04-12
12
193 Views
Last Modified: 2010-08-05
I'm new to C++.  I want to be able to declare a list of variables for a number of years and dollar amounts, etc.  ( Year1, year2, etc).  I dont want to type this for each variable for the total possible number of years.  I tried declaring a variable to reflect the value of a counter.
year(counter);
amount( counter );
At the end of the main program- i tried doing simple math using these variables.
total(counter)=amount(counter)*rate(counter)

when I tried to compile it- I got error c2064, term does not evaluate to a function.  It apparently sees these as a function call.
I dont know how to declare a variable that increments.  Used to be able to do this in programming languages of yesterday.  
anyhelp much appreciated.
Rixx
0
Comment
Question by:rixxagain
  • 6
  • 6
12 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Use square brackets [] for subscripting and array declarations.

e.g.:

long Amount[1];

...

total[counter]=amount[counter]*rate[counter];
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Damn bifocals,

long Amount[10];

whatever...
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility

float amt[10];
float rate[10];
float yrtotals[10];
float grandtot;
int years[10];
int i;

...
for (i=0; i<9; i++)
    {
    yrtotals[i]=amt[i]*rate[i];
    printf("Year %4d total is %f6.2\n",years[i],yrtotals[i]);
    grandtot=grandtot+yrtotals[i];
    }
printf("Grand total is %f6.2\n",grandtot);

0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
PS:  Small world - I was at Goddard in the early 70's.
0
 

Author Comment

by:rixxagain
Comment Utility
I'll try this as soon as possible.
Thanks for the input.
Rixx
0
 

Author Comment

by:rixxagain
Comment Utility
Cookre
   I tried this out and the subscripting works well.  The only other question that I have is-- when I print out the results- I get what looks like HEX, and the grandtot = 0.
Ex:
yield=amt*(rate/100);
total[ctr]=amt+yield;
cout << "The total is:  " <<total <<endl;

it will print:
the total is:  006bfda4
The grandtotal is: 0

Thanks for your patience.  Busy week.
rixxagain

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rixxagain
Comment Utility
Are you still at NASA?
I was only there, at Lewis, about 4years.
Temp position that I got tired of waiting
to go permanent.  Shoulda waited though.
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Nah, I left Goddard in '74 for central North Carolina - where I've stayed.  Still a contractor, though.

You don't show all of the code, so it's a bit difficult to say why, but I'd guess one of the variables wasn't appropriate.


I just did a quick test of this:
int main(int argc, char* argv[])
{
float amt[10];
float rate[10];
float yrtotals[10];
float grandtot;
int years[10];
int i;

for (i=0; i<9; i++)
    {
    amt[i]=(i+1)*10.0;
    rate[i]=1.5;
    years[i]=1995+i;
    }
grandtot=0.0;

for (i=0; i<9; i++)
    {
    yrtotals[i]=amt[i]*rate[i];
    cout << "Year " << years[i] << " total is " << yrtotals[i] <<endl;
    grandtot=grandtot+yrtotals[i];
    }
cout << "The grand total is:  " <<grandtot <<endl;
}

and got:
Year 1995 total is 15
Year 1996 total is 30
Year 1997 total is 45
Year 1998 total is 60
Year 1999 total is 75
Year 2000 total is 90
Year 2001 total is 105
Year 2002 total is 120
Year 2003 total is 135
The grand total is:  675



If you can't figure out what you missed, post your entire program and let's figure it out.
0
 

Author Comment

by:rixxagain
Comment Utility
Ok.  This may look pretty basic to ya. I just started this not long ago.  Some of the stuff on yer program looks martian to me.  Haha.

#include <iostream>
int main()
{
0
 

Author Comment

by:rixxagain
Comment Utility
I dont know what happened above there.  Partial post.

#include <iostream>
int main()
{
   int ctr=0;
   using std::cout;
   using std::cin;
   using std:endl;

   float total[20];
   float yiels;
   float amt;
   float rate;
   int my;
   int years;
   float grandtot=0;

   cout << "enter the number of years: ";
   cin >> years;
   loop:  ctr++;
      cout << "Enter the year: ";
      cin  >> my
      cout >> "Enter the amount invested: ";
      cin >> amt;
      cout << "Enter the rate in percentage: ";
      cin >> rate;
      yield=amt*(rate/100);
      total[ctr]=amt+yield;
      cout << "\nThe yearis: " <<my;
      cout << "\nThe total for the year is: " << total    <<endl;
   if (ctr<years)
      go to loop
   cout << "Grandtotal is: " << grandtot << endl;
   return 0;
}

My end goal is to be able to input the amount invested in any one year, plus the rate of gain in that year, the new total for that year, and total it for all years.  I want to eventually print it all out in a table format, like-
amount invested    rate    yield    new total

And include grandtotal at the bottom.

Is this what you do now cookre?  C++?
Thanks for the help.  This is alot harder than I thought.  
Rixx
 
0
 
LVL 22

Accepted Solution

by:
cookre earned 100 total points
Comment Utility
Doing it that way (i.e., prompting for each year's info from within the loop) means you don't need any arrays to store anything across iterations.

The wierd result for 'total' was because you declared it an array then tried to print 'total', which in C is the memory address of the first element of the array.

The 0 for grandtotal came about because you forgot to keep a running total.

Soo, here's a slight modification.  See if this doesn't get closer:


#include <iostream>
int main()
{
  int ctr=0;
  using std::cout;
  using std::cin;
  using std:endl;

  float total;          // NO LONGER DEFINED AS AN ARRAY
  float yield;   // yield INSTEAD OF yiels
  float amt;
  float rate;
  int my;
  int years;
  float grandtot=0;

  cout << "enter the number of years: ";
  cin >> years;
  loop:  ctr++;
     cout << "Enter the year: ";
     cin  >> my;        // ADDED SEMICOLON
     cout << "Enter the amount invested: "; // << NOT >>
     cin >> amt;
     cout << "Enter the rate in percentage: ";
     cin >> rate;
     yield=amt*(rate/100);
     total=amt+yield;
     cout << "\nThe yearis: " <<my;
     cout << "\nThe total for the year is: " << total    <<endl;
     grandtot=grandtot+total;  // KEEP RUNNING TOTAL
  if (ctr<years)
     goto loop;     // goto INSTEAD OF go to AND ADDED SEMICOLON
  cout << "Grandtotal is: " << grandtot << endl;
  return 0;
}


---
Yeah, I'm still a programmer after 30 some odd years.  I doubled as manager of the local office for five or so years (Computer Sciences Corp, Reasearch Triangle Park, NC), but never really enjoyed it that much.  Right now I'm a contract programmer for the Postal Service in Raleigh (12 years, so far) and thoroughly enjoying it.
0
 

Author Comment

by:rixxagain
Comment Utility
That did it cookre.  After I made those changes- works like a charm.
I have another question to do with comparing character strings that I'll open up another thread for, so I can issue new points.  Like to see ya there.
Thanks again
Rixx
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now