This message was edited by john_samer at 2006-4-6 15:30:48
I am using Visual Studio 2005 and the program is in
C++ code. The program runs at first but, after the
user types their name and presses return, an error
message appears saying that the program (the one I
made) needs to be closed. On running through on a
debug I find that it says that a variable called
"this" cannot be found. There is no variable, command
or anything called "this" in the code. Now,
previously, I had written some pseudo code in a text
editter/word processer. This contained that word. I
then rewrote it in C++ code before copying and pasting
it into Visual Studio.
The code asks for a person's name (initials and
surname) and seperates it into their initials and
surname. This is where the problem is. The rest of the
code appears to work fine. I have tried cutting and
pasting into different text programs and then cutting
and pasting it into a new cpp file and the problem
persists. When I debug it a message appears saying
"unauthorised access to a variable" and prompts me to
load the string concatenation assembly code. The
string concatenation command is where the error is.
This may have been where "this" was
Here is the code
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <math.h>
void seperateWords(char string[26], char wordArray[3][20])
{
char charInQ = ' ';
int currentChar = 0;
int currentCharOfWord = 0;
int currentWord = 0;
while (currentWord <= 2)
{
charInQ = string[currentChar];
if (charInQ != ' ')
{
wordArray[currentWord][currentCharOfWord] = charInQ;
currentCharOfWord++;
}
else
{
currentWord++;
currentCharOfWord = 0;
}
currentChar++;
}
}
void seperateName(char name[26], char initials[5], char surname[20])
{
char nameArray[3][20];
seperateWords(name, nameArray);
if (!strcmp(nameArray[2], ""))
{
strcpy(surname, nameArray[2]);
strcpy(initials, nameArray[0]);
strcat(initials, nameArray[1]);
}
else
{
strcpy(surname, nameArray[1]);
strcpy(initials, nameArray[0]);
}
}
bool IsValidPostcode(char postcode[7])
{
bool valid = true;
if( !((postcode[0] >= 65 && postcode[0] <= 90) || (postcode[0] >= 97 && postcode[0] <= 122)) )
{
valid = false;
}
if( !((postcode[1] >= 65 && postcode[1] <= 90) || (postcode[1] >= 97 && postcode[1] <= 122)) )
{
valid = false;
}
if( !(postcode[2] >= 48 && postcode[2] <= 57) )
{
valid = false;
}
if( postcode[3] != ' ' )
{
valid = false;
}
if( !(postcode[4] >= 48 && postcode[4] <= 57) )
{
valid = false;
}
if( !((postcode[5] >= 65 && postcode[5] <= 90) || (postcode[5] >= 97 && postcode[5] <= 122)) )
{
valid = false;
}
if( !((postcode[6] >= 65 && postcode[6] <= 90) || (postcode[6] >= 97 && postcode[6] <= 122)) )
{
valid = false;
}
return valid;
}
float calculateVAT(float cost)
{
float rateOfVAT = 0.175f;
cost = cost + ceil(cost * rateOfVAT);
return cost;
}
float calculateDeliveryCharge(int numberOfBottles)
{
float deliveryCharge = 0.0f;
float amountToPrice[5][3] = { {01.0f, 07.0f, 7.95f},
{08.0f, 14.0f, 4.95f},
{15.0f, 21.0f, 3.95f},
{22.0f, 28.0f, 1.95f},
{29.0f, 00.0f, 0.00f} };
int amountToPriceRowAmount = 5;
return deliveryCharge;
}
float calculateCost(int numberOfBottles)
{
float bottlePrice = 3.00f;
float cost;
float costPlusVAT;
float totalCost;
cost = bottlePrice * numberOfBottles;
costPlusVAT = calculateVAT(cost);
totalCost = costPlusVAT + calculateDeliveryCharge(numberOfBottles);
return totalCost;
}
void displayOrder(char initials[5], char surname[20], char address[100], char postcode[7], int numberOfBottles, float cost, float costPlusVAT, float deliveryCharge, float totalCost)
{
cout << "initials: " << initials << endl;
cout << "surname: " << surname << endl;
cout << "address: " << address << endl;
cout << "postcode: " << postcode << endl;
cout << "numberOfBottles: " << numberOfBottles << endl;
cout << "cost: " << cost << endl;
cout << "costPlusVAT: " << costPlusVAT << endl;
cout << "deliveryCharge: " << deliveryCharge << endl;
cout << "totalCost: " << totalCost << endl;
}
bool enterAnotherOrder()
{
char truthChar;
while (true)
{
cout << "Do you want to enter another order?" << endl;
cin >> truthChar;
if (truthChar == 'Y' || truthChar == 'y')
{
return true;
}
else if (truthChar == 'N' || truthChar == 'n')
{
return false;
}
}
}
void main()
{
char name[26];
char initials[5];
char surname[20];
char address[100];
char postcode[7];
int amountToPriceRowAmount = 5;
int numberOfBottles;
float cost;
float costPlusVAT;
float deliveryCharge;
float totalCost;
cout << "Please write your name." << endl;
cin >> name;
seperateName(name, initials, surname);
if(!strcmp(initials, ""))
{
cout << "An error has occured. You did not type your initials." << endl;
while(!kbhit());
}
if(!strcmp(surname, ""))
{
cout << "An error has occured. You did not enter a surname." << endl;
while(!kbhit());
}
cout << "Please write your address." << endl;
cin >> address;
cout << "Please write your post code." << endl;
cin >> postcode;
if(!IsValidPostcode(postcode))
{
cout << "An error has occured. You typed an invalid postcode." << endl;
while(!kbhit());
}
cout << "How many bottles would you like to order?" << endl;
cin >> numberOfBottles;
if(numberOfBottles <= 0)
{
cout << "An error has occured. We cannot suply you with fewer than 1 bottle." << endl;
while(!kbhit());
}
calculateCost(numberOfBottles);
displayOrder(initials, surname, address, postcode, numberOfBottles, cost, costPlusVAT, deliveryCharge, totalCost);
enterAnotherOrder();
while(!kbhit());
}