Thread: Need help with a class assignment

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    Need help with a class assignment

    I was given a class assignment over my spring break (which is this week), and I am having trouble doing the programming warm up exercises, and I was wondering if someone could help me out. I used Visual Studio 2010 to make this program and the programming language is C++ (the class I am taking for it is Software Design and Development [which is basically an intro to C++ class]).

    1) After it completes the test for "PWU Exercise 7", it seems to crash for some reason, but I cannot decipher why

    2) 2) For PWU Exercise 9, I am supposed to write a function that will ask the user to input a value that will be used to create the length of an array. However, since I can only use an immediate or constant expression as the length initializer, I am not sure how I can do so. I tried defining a constant that is initialized to the variable that contains the input from the user, but that did not work.


    Code:
    // InClass Assignment 5
    // 3/11/2011
    // 
    
    #include <iostream>
    
    #include <cstring>
    
    #include <string>
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include "Phone.h"
    
    using namespace std;
    
    bool ShallowCompare (Phone*, Phone*);
    
    bool DeepCompare (Phone*, Phone*);
    
    int Greatest (int[], int);
    
    int GetGreatestInput();
    
    int main()
    {
    	int static max;
    
    	// PWU Exercise 1
    	int someInt;
    	
    	int* intPointer;
    
    	intPointer = &someInt;
    
    	someInt = 451;
    
    	if(someInt == 451)
    		cout << "Passed PWU Exercise 1." << endl;
    	else
    		cout << "Failed PWU Exercise 1." << endl;
    
    	someInt = 0;
    
    	*intPointer = 451;
    
    	if(someInt == 451)
    		cout << "Passed PWU Exercise 1.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 1.\n" << endl;
    
    	// PWU Exercise 2
    	char* charArrPointer;
    
    	char initials[4];
    
    	charArrPointer = initials;
    
    	*charArrPointer = 'A';
    
    	*(charArrPointer + 1) = 'E';
    
    	*(charArrPointer + 2) = 'W';
    
    	if (*charArrPointer == 'A')
    		cout << "Passed PWU Exercise 2.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 2.\n" << endl;
    
    	// PWU Exercise 3
    	Phone newPhone;
    
    	Phone* structPointer = &newPhone;
    
    	structPointer->country = 1;
    
    	structPointer->area = 888;
    
    	structPointer->number = 5551212;
    
    	if ((structPointer->country == 1) && (structPointer->area == 888) && (structPointer-> number == 5551212))
    	{
    		cout << "Passed PWU Exercise 3.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 3.\n" << endl;
    	}
    
    	// PWU Exercise 4
    	Phone& structReference = newPhone;
    
    	structReference.country = 1;
    
    	structReference.area = 888;
    
    	structReference.number = 5551212;
    
    	if ((structReference.country == 1) && (structReference.area == 888) && (structReference.number == 5551212))
    	{
    		cout << "Passed PWU Exercise 4.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 4.\n" << endl;
    	}
    
    	// PWU Exercise 5
    	Phone* structPointer1 = &newPhone;
    
    	Phone* structPointer2 = &newPhone;
    	
    	if (ShallowCompare(structPointer1,structPointer2))
    	{
    		cout << "Passed PWU Exercise 5.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 5.\n" << endl;
    	}
    
    	// PWU Exercise 6
    	Phone* structPointer3 = &newPhone;
    
    	Phone* structPointer4 = &newPhone;
    
    	if (DeepCompare(structPointer3, structPointer4))
    	{
    		cout << "Passed PWU Exercise 6.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 6.\n" << endl;
    	}
    
    	// PWU Exercise 7
    	int exercise7Array[100];
    
    	int* dataPtr = exercise7Array;
    
    	for (int count = 0; count < 100; count++)
    	{
    		*(dataPtr + count) = count;
    	}
    
    	for (int count = 0; count < 100; count++)
    	{
    		int temporary;
    
    		temporary = *(dataPtr + count);
    
    		if (count == 0)
    		{
    			temporary = max;
    		}
    
    		else
    		{
    			if (max < temporary)
    			{
    				max = temporary;
    			}
    		}
    	}
    
    	if (max == 99)
    		cout << "Passed PWU Exercise 7.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 7.\n" << endl;
    
    	delete dataPtr;
    
    	// PWU Exercise 8
    	int exercise8Array[100];
    
    	int* dataPtr2 = exercise8Array;
    
    	for (int count = 0; count < 100; count++)
    	{
    		*(dataPtr2 + count) = count;
    	}
    
    	max = Greatest(exercise8Array, 100);
    
    	if (max == 99)
    		cout << "Passed PWU Exercise 8.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 8.\n" << endl;
    
    	// PWU Exercise 9
    	int largestValue9 = GetGreatestInput();
    
    	if (largestValue9 == 99)
    		cout << "Passed PWU Exercise 9.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 9.\n" << endl;
    
    	// PWU Exercise 10
    
    
    	// PWU Exercise 11
    	int* oldValue;
    
    	int* newValue;
    
    	int exercise10Int1 = 40;
    
    	int exercise10Int2 = 50;
    
    	oldValue = &exercise10Int1;
    
    	if (*oldValue != NULL)
    	{
    		newValue = oldValue;
    	}
    
    	else
    	{
    		newValue = &exercise10Int2;
    	}
    
    	if (*newValue == 50)
    	{
    		cout << "Passed PWU Exercise 11.\n" << endl;
    	}
    
    	else
    	{
    		cout << "Failed PWU Exercise 11.\n" << endl;
    	}
    
    	// PWU Exercise 12
    	if (*oldValue == *newValue)
    	{
    		delete oldValue;
    	}
    
    	if (*oldValue != NULL)
    	{
    		cout << "Passed PWU Exercise 11.\n" << endl;
    	}
    
    	else
    	{
    		cout << "Failed PWU Exercise 11.\n" << endl;
    	}
    
    	// PWU Exercise 13
    
    
    	// PWU Exercise 14
    
    
    	// PWU Exercise 15
    
    
    	// PWU Exercise 16
    
    
    	// PWU Exercise 17
    
    
    	// PWU Exercise 18
    
    
    	// PWU Exercise 19
    
    
    	// PWU Exercise 20
    
    
    	system("Pause");
    
    	return 0;
    }
    
    bool ShallowCompare (Phone* ptr1, Phone* ptr2)
    {
    	if(ptr1 == ptr2)
    		return true;
    	else
    		return false;
    }
    
    bool DeepCompare (Phone* ptr1, Phone* ptr2)
    {
    	if ((ptr1->country == ptr2->country) && (ptr1->area == ptr2->area) && (ptr1->number == ptr2->number))
    		return true;
    	else
    		return false;
    }
    
    int Greatest (int array1[], int size)
    {
    	int maximum;
    
    	int* temporaryPtr = array1;
    
    	for (int count = 0; count < size; count++)
    	{
    		int temporary;
    
    		temporary = *(temporaryPtr + count);
    
    		if (count == 0)
    		{
    			maximum = temporary;
    		}
    
    		else
    		{
    			if (maximum < temporary)
    			{
    				maximum = temporary;
    			}
    		}
    	}
    
    	delete temporaryPtr;
    
    	return maximum;
    }
    
    int GetGreatestInput()
    {
    	int size;
    
    	cout << "Input the number of values to be input: " << endl;
    
    	cin >> size;
    
    	cin.ignore(100, '\n');
    
    	int temporaryArray[1000];
    
    	for (int count = 0; count < size; count++)
    	{
    		int temporaryValue;
    
    		cout << "Input an integer value to be put into the array: " << endl;
    
    		cin >> temporaryValue;
    
    		cin.ignore(100, '\n');
    
    		temporaryArray[count] = temporaryValue;
    	}
    
    	int largestValue = Greatest(temporaryArray, size);
    
    	return largestValue;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    For Ex7, you called delete, but you never called new.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Your first problem is related to these 3 lines of code:
    Code:
    int exercise7Array[100];
    int* dataPtr = exercise7Array;
    delete dataPtr;
    You are trying to delete memory that was allocated on the stack (not allocated with new).

    For the second problem read about dynamic allocations here: http://en.wikipedia.org/wiki/New_(C%2B%2B)

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Okay thanks, that cleared up the problems I had with (1), but can you guys or someone else help me with (2)?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For a dynamic length array, you can use std::vector.
    std::vector<mytype> myvec(my_size);

    >>bool ShallowCompare (Phone*, Phone*);
    Regarding this, however... don't do that.
    Read more here: http://sourceforge.net/apps/mediawik...arameter_names
    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.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    But the thing is, we haven't covered vectors yet (it is the next chapter I think), so I don't think I am supposed to use vectors in any of these Programming Warm Up Exercises.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *shrug* It is the way to do it in C++ programs. But for experience, I guess, you may use new.

    Mytype* p = new mytype[n]; // Where n is a variable whose size you want the array to be
    // ...
    delete [] p; // Do NOT forget to delete!

    That also brings up smart pointers, which you class may or may not cover. You can find some information about them here to read up on later if case your class do not cover them:
    SourceForge.net: Raw pointer issues - cpwiki
    They are extremely important in C++ programming.

    There are more problems in the code:
    As pointed out already, there is a problem here:
    Code:
    	int exercise7Array[100];
    	int* dataPtr = exercise7Array;
    	// ...
    	delete dataPtr;
    Do not call delete on anything you did not get from new.

    In Greatest, you have the same problem:
    Code:
    	int* temporaryPtr = array1;
    	delete temporaryPtr;
    Code:
    if (*oldValue != NULL)
    This is wrong, wrong, wrong. First off, never use NULL.
    In proper code, all pointers will be initialized to nullptr (or better yet, a smart pointer is used). To test if a pointer is valid then, you check if it's not nullptr, or just if (p) where p is a pointer.
    Comparing a dereferenced pointer (which does not point to another point) is wrong. You would realize this if you used nullptr instead of NULL (macros are evil).

    So your logic there is wrong. You need to look at that and fix it.

    Code:
    	if (*oldValue == *newValue)
    	{
    		delete oldValue;
    	}
    Wrong, wrong, wrong! Do not delete what you did not new!
    For that matter, deleting something does not set it to null. You must do that manually, like
    OldValue = nullptr;

    Code:
    if (*oldValue != NULL)
    Same problem as before.

    Code:
    system("Pause");
    See SourceForge.net: Pause console - cpwiki
    Last edited by Elysia; 03-16-2011 at 09:20 AM.
    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.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    In regards to some of your evaluations, I have updated my code and it seems to be working fine.

    Code:
    // InClass Assignment 5
    // 3/11/2011
    //
    
    #include <iostream>
    
    #include <cstring>
    
    #include <string>
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include "Phone.h"
    
    using namespace std;
    
    bool ShallowCompare (Phone*, Phone*);
    
    bool DeepCompare (Phone*, Phone*);
    
    int Greatest (int*, int);
    
    int GetGreatestInput();
    
    int main()
    {
    	int static max;
    
    	// PWU Exercise 1
    	int someInt;
    	
    	int* intPointer;
    
    	intPointer = &someInt;
    
    	someInt = 451;
    
    	if(someInt == 451)
    		cout << "Passed PWU Exercise 1." << endl;
    	else
    		cout << "Failed PWU Exercise 1." << endl;
    
    	someInt = 0;
    
    	*intPointer = 451;
    
    	if(someInt == 451)
    		cout << "Passed PWU Exercise 1.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 1.\n" << endl;
    
    	// PWU Exercise 2
    	char* charArrPointer;
    
    	char initials[4];
    
    	charArrPointer = initials;
    
    	*charArrPointer = 'A';
    
    	*(charArrPointer + 1) = 'E';
    
    	*(charArrPointer + 2) = 'W';
    
    	if (*charArrPointer == 'A')
    		cout << "Passed PWU Exercise 2.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 2.\n" << endl;
    
    	// PWU Exercise 3
    	Phone newPhone;
    
    	Phone* structPointer = &newPhone;
    
    	structPointer->country = 1;
    
    	structPointer->area = 888;
    
    	structPointer->number = 5551212;
    
    	if ((structPointer->country == 1) && (structPointer->area == 888) && (structPointer-> number == 5551212))
    	{
    		cout << "Passed PWU Exercise 3.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 3.\n" << endl;
    	}
    
    	// PWU Exercise 4
    	Phone& structReference = newPhone;
    
    	structReference.country = 1;
    
    	structReference.area = 888;
    
    	structReference.number = 5551212;
    
    	if ((structReference.country == 1) && (structReference.area == 888) && (structReference.number == 5551212))
    	{
    		cout << "Passed PWU Exercise 4.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 4.\n" << endl;
    	}
    
    	// PWU Exercise 5
    	Phone* structPointer1 = &newPhone;
    
    	Phone* structPointer2 = &newPhone;
    	
    	if (ShallowCompare(structPointer1,structPointer2))
    	{
    		cout << "Passed PWU Exercise 5.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 5.\n" << endl;
    	}
    
    	// PWU Exercise 6
    	Phone* structPointer3 = &newPhone;
    
    	Phone* structPointer4 = &newPhone;
    
    	if (DeepCompare(structPointer3, structPointer4))
    	{
    		cout << "Passed PWU Exercise 6.\n" << endl;
    	}
    	else
    	{
    		cout << "Failed PWU Exercise 6.\n" << endl;
    	}
    
    	// PWU Exercise 7
    	
    	int* dataPtr = new int[100];
    
    	for (int count = 0; count < 100; count++)
    	{
    		*(dataPtr + count) = count;
    	}
    
    	for (int count = 0; count < 100; count++)
    	{
    		int temporary;
    
    		temporary = *(dataPtr + count);
    
    		if (count == 0)
    		{
    			temporary = max;
    		}
    
    		else
    		{
    			if (max < temporary)
    			{
    				max = temporary;
    			}
    		}
    	}
    
    	if (max == 99)
    		cout << "Passed PWU Exercise 7.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 7.\n" << endl;
    
    	delete dataPtr;
    
    	// PWU Exercise 8
    	int* dataPtr2 = new int[100];
    
    	for (int count = 0; count < 100; count++)
    	{
    		*(dataPtr2 + count) = count;
    	}
    
    	max = Greatest(dataPtr2, 100);
    
    	if (max == 99)
    		cout << "Passed PWU Exercise 8.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 8.\n" << endl;
    
    	// PWU Exercise 9
    	int largestValue9 = GetGreatestInput();
    
    	if (largestValue9 == 99)
    		cout << "Passed PWU Exercise 9.\n" << endl;
    	else
    		cout << "Failed PWU Exercise 9.\n" << endl;
    
    	// PWU Exercise 10
    
    
    	// PWU Exercise 11
    	int* oldValue = new int;
    
    	int* newValue = new int;
    
    	*oldValue = 40;
    
    	if (*oldValue != NULL)
    	{
    		*newValue = *oldValue;
    	}
    
    	else
    	{
    		*newValue = 50;
    	}
    
    	if (*newValue == 40)
    	{
    		cout << "Passed PWU Exercise 11.\n" << endl;
    	}
    
    	else
    	{
    		cout << "Failed PWU Exercise 11.\n" << endl;
    	}
    
    	// PWU Exercise 12
    	if (*oldValue == *newValue)
    	{
    		delete oldValue;
    	}
    
    	if (oldValue != nullptr)
    	{
    		cout << "Passed PWU Exercise 12.\n" << endl;
    	}
    
    	else
    	{
    		cout << "Failed PWU Exercise 12.\n" << endl;
    	}
    
    	// PWU Exercise 13
    
    
    	// PWU Exercise 14
    
    
    	// PWU Exercise 15
    
    
    	// PWU Exercise 16
    
    
    	// PWU Exercise 17
    
    
    	// PWU Exercise 18
    
    
    	// PWU Exercise 19
    
    
    	// PWU Exercise 20
    
    
    	system("Pause");
    
    	return 0;
    }
    
    bool ShallowCompare (Phone* ptr1, Phone* ptr2)
    {
    	if(ptr1 == ptr2)
    		return true;
    	else
    		return false;
    }
    
    bool DeepCompare (Phone* ptr1, Phone* ptr2)
    {
    	if ((ptr1->country == ptr2->country) && (ptr1->area == ptr2->area) && (ptr1->number == ptr2->number))
    		return true;
    	else
    		return false;
    }
    
    int Greatest (int* temporaryPtr, int size)
    {
    	int maximum;
    
    	for (int count = 0; count < size; count++)
    	{
    		int temporary;
    
    		temporary = *(temporaryPtr + count);
    
    		if (count == 0)
    		{
    			maximum = temporary;
    		}
    
    		else
    		{
    			if (maximum < temporary)
    			{
    				maximum = temporary;
    			}
    		}
    	}
    
    	delete temporaryPtr;
    
    	return maximum;
    }
    
    int GetGreatestInput()
    {
    	int size;
    
    	cout << "Input the number of values to be input: " << endl;
    
    	cin >> size;
    
    	cin.ignore(100, '\n');
    
    	int* temporaryPtr = new int[size];
    
    	for (int count = 0; count < size; count++)
    	{
    		int temporaryValue;
    
    		cout << "Input an integer value to be put into the array: " << endl;
    
    		cin >> temporaryValue;
    
    		cin.ignore(100, '\n');
    
    		*(temporaryPtr + count) = temporaryValue;
    	}
    
    	int largestValue = Greatest(temporaryPtr, size);
    
    	return largestValue;
    }
    Last edited by EonsNearby; 03-16-2011 at 09:51 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What did you change? Abovementioned problems still exist.
    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.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Sorry, posted the old code. It has been changed

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe you should post the updated code then?
    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.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    I did, it is in the post where I had posted the old code by mistake. I edited that post so now it has the new code in it.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I still see a lot of erroneous deletes. And the prototypes have not been changed either.
    All of which I pointed out earlier.
    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.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Crossposting is generally frowned upon.
    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. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. i need help with errors in my assignment for class
    By azrael13302473 in forum C++ Programming
    Replies: 1
    Last Post: 07-04-2002, 06:02 PM