Scope problem.

This is a discussion on Scope problem. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to use pointers as the parameters of a function. The pointers are supposed to point to the ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Scope problem.

    Hi,

    I'm trying to use pointers as the parameters of a function. The pointers are supposed to point to the items held within a text file which was previously created by the use of an array of structs.

    This is what I've done so far:-

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    struct items
    {
    	double price;
    	bool luxury;
    };
    
    void FunctionsName(double *pointer2price, bool *pointer2luxury);
    
    int main()
    {
    	items StructArrayName[10];
    	
    	ifstream StreamName;
    	StreamName.open("myfile2.txt");
    
    	if(!StreamName)
    	{
    		cout << "phail " << endl;
    	}
    
    	for(int i = 0;i < 10; i++)
    	{
    		StreamName >> StructArrayName[i].price >> StructArrayName[i].luxury;
    	}
    
    
    	double *pointer2price;
    	StructArrayName->price = *pointer2price;//uninitialized local variable 'pointer2price' used
    
    	bool *pointer2luxury;
    	StructArrayName->luxury = *pointer2luxury;//uninitialized local variable 'pointer2luxury' used
    
    
    	StreamName.close();
    
    	system ("pause");
    	return 0;
    }
    void FunctionsName(double *pointer2price, bool *pointer2luxury)
    {
    	for(int a = 0;a<10;a++)
    	{
    		if(*pointer2price>9.99)
    		{
    			*pointer2price = 9.95;
    		}
    		if(*pointer2luxury = 1)
    		{
    			*pointer2luxury = 0;
    		}
    	}
    }
    My question is just in regards to the bold text errors. I'm unsure on how to combine having the pointers point to the struct's items, while still having them accessible by a function call (which I haven't yet placed into main).

    .....I'm hoping that makes sense. Many thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    instead of this
    Code:
    	double *pointer2price;
    	StructArrayName->price = *pointer2price;//uninitialized local variable 'pointer2price' used
    
    	bool *pointer2luxury;
    	StructArrayName->luxury = *pointer2luxury;//uninitialized local variable 'pointer2luxury' used
    you probabli want this
    Code:
    FunctionsName(&StructArrayName->price ,&StructArrayName->luxury );
    but this is C++ forum - so usage of referencies is more suitable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Code:
    	double *pointer2price;
    	StructArrayName->price = *pointer2price;//uninitialized local variable 'pointer2price' used
    
    	bool *pointer2luxury;
    	StructArrayName->luxury = *pointer2luxury;//uninitialized local variable 'pointer2luxury' used
    This is bad for two reasons and Visual Studio is nice enough to let you know about it.
    Firstly, those pointers are uninitialized. Meaning that they will contain garbage. You'll never get a sensible result from that.
    Second is that they're pointers and you're trying to dereference them. This makes it a 99&#37; chance you'll get a crash because they point to some garbage.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Binary Search Tree scope? problem
    By tms43 in forum C++ Programming
    Replies: 5
    Last Post: 11-01-2006, 10:13 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21