Solved

New to C++, how to list variables?

Posted on 2002-04-12
12
205 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
ID: 6938389
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
ID: 6938391
Damn bifocals,

long Amount[10];

whatever...
0
 
LVL 22

Expert Comment

by:cookre
ID: 6938406

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 22

Expert Comment

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

Author Comment

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

Author Comment

by:rixxagain
ID: 6952602
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
 

Author Comment

by:rixxagain
ID: 6952619
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
ID: 6955175
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
ID: 6955364
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
ID: 6955412
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
ID: 6955493
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
ID: 6957267
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.

770 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