Quantcast

Maximum PC

It is currently Thu Oct 23, 2014 11:22 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: C++ array/pointer question
PostPosted: Tue Dec 14, 2004 6:53 pm 
Little Foot
Little Foot

Joined: Wed Nov 10, 2004 7:28 am
Posts: 155
It's been a while since I have used C++. Today I decided to brush up on it and made a smiple calculation program with an array. However when i go to display the info from the array i get only addresslocations not the actual value in the position. here is the code remember i said it was simple just for my minds value.
Code:
#include "stdafx.h"

main(void)
{
   int nums[101];
   double userinput;
   double output[101];
   int cnt;
   char hold;
   for (cnt =0; cnt <= 99; cnt++);
   {
      nums [cnt] = nums[cnt] + 1;
   }
   std::cout << "Please enter any number" << std::endl;
   std::cin >> userinput;
   for (cnt = 0; cnt <=99; cnt++)
   {
      double test;
      output[cnt] = nums[cnt] * userinput;
      test -> output[cnt];

      std::cout << "The value for spot " << cnt << " is" << test << std::endl;
   }
   std::cout << "Halting the program for information" << std::endl;
   std::cin >> hold;
      return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 7:40 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Try *test.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 7:53 pm 
Little Foot
Little Foot

Joined: Wed Nov 10, 2004 7:28 am
Posts: 155
i get compiler error illegal indirection


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 9:00 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
And I get "error C2227: left of '->output' must point to class/struct/union".

Why not just use an assignment? test = output[cnt];


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 10:23 pm 
Little Foot
Little Foot

Joined: Wed Nov 10, 2004 7:28 am
Posts: 155
ya i did have it as = i forgot to change it back after testing before putting it up here.


Top
  Profile  
 
 Post subject: Re: C++ array/pointer question
PostPosted: Wed Dec 15, 2004 11:35 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000

Joined: Mon Jun 14, 2004 2:50 pm
Posts: 45
Location: Rochester, NY
Your problem is that you have a semi-colon after your for-statement.

Remove it.

All better.

Code:
#include <iostream>

using namespace std;

int main(void) {
    int nums[101];
    double userinput;
    double output[101];
    int cnt;
    char hold;
   
    for (cnt =0; cnt <= 99; cnt++)
        nums [cnt] = nums[cnt] + 1;
    cout << "Please enter any number" << endl;
    cin >> userinput;
    for (cnt = 0; cnt <=99; cnt++) {
        double test;
        output[cnt] = nums[cnt] * userinput;
        test = output[cnt];
        cout << "The value for spot " << cnt << " is" << test << endl;
    }
    cout << "Halting the program for information" << endl;
    cin >> hold;
    return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 12:07 pm 
Little Foot
Little Foot

Joined: Wed Nov 10, 2004 7:28 am
Posts: 155
that wasnt the problem. I acutally put that in later to as a test heres the code as it is now
Code:
#include "stdafx.h"

main(void)
{
   int nums[101];
   double userinput;
   double output[101];
   int cnt;
   char hold;
   for (cnt =0; cnt <= 99; cnt++)
   {
      nums [cnt] = nums[cnt] + 1;
   }
   std::cout << "Please enter any number" << std::endl;
   std::cin >> userinput;
   for (cnt = 0; cnt <=99; cnt++)
   {
      double test;
      output[cnt] = nums[cnt] * userinput;
      test = output[cnt];

      std::cout << "The value for spot " << cnt << " is" << test << std::endl;
   }
   std::cout << "Halting the program for information" << std::endl;
   std::cin >> hold;
      return 0;
}




and this what i get onscreen when i run it i'll start at 98 to make it easer for me
The value for spot 98 is -2.57698e+009
The Value for spot 99 is -2.57698e+009
I know that that is * 10^-9 and this is when i enter 3 as the value its giving me the memory address isnt it.


Top
  Profile  
 
 Post subject: Re: C++ array/pointer question
PostPosted: Wed Dec 15, 2004 12:24 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24226
Location: Granite Heaven
Phalanx wrote:
Your problem is that you have a semi-colon after your for-statement.

Remove it.

All better.


It is a one line for-loop ... a semi-colon is appropriate.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 1:50 pm 
Little Foot
Little Foot

Joined: Wed Nov 10, 2004 7:28 am
Posts: 155
ahh convention discussion still no answer though.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 2:46 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
No, it's not giving a memory address.

Your problem is that you don't initialize the values. Try this:
Code:

int main(void)
{
   int nums[100]={0};//no need for 101...
   //you must initialize it to a value if you want to use it like this
   double userinput;
   double output[100];
   int cnt;
   char hold;
   for (cnt =0; cnt <= 99; ++cnt)
   {
      nums[cnt] += cnt;
   }
   std::cout << "Please enter any number" << std::endl;
   std::cin >> userinput;
   std::cin.ignore(1000,'\n');//ignore the newline

   for (cnt = 0; cnt <=99; ++cnt)
   {
      output[cnt] = nums[cnt] * userinput;
      std::cout << "The value for spot " << cnt << " is " << output[cnt] << std::endl;
   }

   std::cout << "Halting the program for information" << std::endl;
   std::cin.get();

      return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 4:11 pm 
Little Foot
Little Foot

Joined: Wed Nov 10, 2004 7:28 am
Posts: 155
I understand now thankyou verymuch kybo for the help. I've spent the semester in VB so it didnt even occur to me to intialize the values.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 4:24 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Sure.


Top
  Profile  
 
 Post subject: Re: C++ array/pointer question
PostPosted: Wed Dec 15, 2004 7:34 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000

Joined: Mon Jun 14, 2004 2:50 pm
Posts: 45
Location: Rochester, NY
Jipstyle wrote:
Phalanx wrote:
Your problem is that you have a semi-colon after your for-statement.

Remove it.

All better.


It is a one line for-loop ... a semi-colon is appropriate.


You're correct. I had to do some reformatting when I tried to compile it under Borland and forgot about that syntax.


Top
  Profile  
 
 Post subject: Re: C++ array/pointer question
PostPosted: Thu Dec 16, 2004 8:29 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24226
Location: Granite Heaven
Phalanx wrote:
Jipstyle wrote:
Phalanx wrote:
Your problem is that you have a semi-colon after your for-statement.

Remove it.

All better.


It is a one line for-loop ... a semi-colon is appropriate.


You're correct.


<Homer> Woooohooo!!! </Homer>

<Marge> Enjoy it, Homie ... you know I'll be right next time. </Marge>

<Homer> D'oh! </Homer>

:D


Top
  Profile  
 
 Post subject: Re: C++ array/pointer question
PostPosted: Thu Dec 23, 2004 2:39 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Jipstyle wrote:
Phalanx wrote:
Your problem is that you have a semi-colon after your for-statement.

Remove it.

All better.


It is a one line for-loop ... a semi-colon is appropriate.

Someone needs to kick you in the butt. :)


Top
  Profile  
 
 Post subject: Re: C++ array/pointer question
PostPosted: Thu Dec 23, 2004 2:44 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Nothing like a review to work out the kinks, eh? :)

You had a few problems in your original code... besides NOT using K&R. :P

1. for (cnt =0; cnt <= 99; cnt++);
Yep, the semicolon doesn't belong there.


2. nums [cnt] = nums[cnt] + 1;
Also pointed out - you need to initialize the value here otherwise you're incrementing whatever was previously left in memory by one.

3. double test; ..; test -> output[cnt];
Here you declare a double called test and then treat it like a pointer to a double. I'm actually surprised that this error would compile.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 4:48 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
3. I pointed this out, and he explained he had compiled it, then changed it and posted it and changed it back. It wouldn't compile with that ;)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Dec 25, 2004 5:24 pm 
Little Foot
Little Foot

Joined: Wed Nov 10, 2004 7:28 am
Posts: 155
Well seeing as im just playing a round with C++ right now
1.) Added that in a test to see what would happend
2.) Completely forgot about intializing arrays in C++ (havent had java but im guessing that too and probably every other language cept VB.net)
3.) Was just fooling around with that.
Also I dont know what K&R is but im guessing its a C++ convention? Please remember im a first year Comp Sci major so I have alot of learning to do.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Dec 25, 2004 8:34 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
It's actually a C convention by two authors:
Kernighan and Richie.

http://www.softpanorama.org/Bookshelf/C ... book.shtml


Top
  Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 12:24 pm 
8086
8086

Joined: Wed Dec 22, 2004 1:56 pm
Posts: 4
Location: The Great Northwest
Hi,

Was looking at the posts.

The solution indeed is in the initialization, however the fix that was posted

for (cnt =0; cnt <= 99; ++cnt)
{
nums[cnt] += cnt;
}


will most likely suffer from the same problems. The += adds "cnt" to its original value, which is still whatever happens to be in memory when you declared the array.

Something like this will work:

for (cnt =0; cnt <= 99; ++cnt)
{
nums[cnt] = cnt+1;
}

(I assume that you want the array to have the values 1-100? Btw, if so, you only need declare a [100] size array, not [101]).

Another simpler option is to use the memset() method. This will zero out the memory in the array and effectively make the array initialized to all zeroes - then you can add whatever you want in it (I'm at my in-laws house on a Win98SE box btw, so no access to any compilers or reference - you'll have to look up memset).

Hope that helps
-a


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.