Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

C++ or text editor error

john_samerjohn_samer Member Posts: 2
[b][red]This message was edited by john_samer at 2006-4-6 15:30:48[/red][/b][hr]
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
#include
#include
#include

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());
}

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    Did you read and correct the errrors and warnings? I tried to compile with 2005 Express and your code prodced millions of problems.

    1. Replace the header file with because iostream.h is obsolete.
    2. add "using namespace" at the end of the includes
    3. replace math.h with cmath

    your includes should look like this. The pragma turns off the silly warnings about functions in cstring being deprecated. They can safely be ignored.
    [code]
    #include "stdafx.h"
    #pragma warning(disable: 4996)
    #include
    #include
    #include
    #include
    using namespace std;
    [/code]

    what remains are 4 warnings about using variables before they are initialized. you definitely must correct those warnings (really are errors).

  • john_samerjohn_samer Member Posts: 2
    : Did you read and correct the errrors and warnings? I tried to compile with 2005 Express and your code prodced millions of problems.
    :
    : 1. Replace the header file with because iostream.h is obsolete.
    : 2. add "using namespace" at the end of the includes
    : 3. replace math.h with cmath
    :
    : your includes should look like this. The pragma turns off the silly warnings about functions in cstring being deprecated. They can safely be ignored.
    : [code]
    : #include "stdafx.h"
    : #pragma warning(disable: 4996)
    : #include
    : #include
    : #include
    : #include
    : using namespace std;
    : [/code]
    :
    : what remains are 4 warnings about using variables before they are initialized. you definitely must correct those warnings (really are errors).
    :
    :
    I have fixed those errors but the same problem keeps happening. The program compiles with no errors but, while running it, after the user inputs their name, it says that I must close the program (the one I made) as I am illegally accessing a variable called 'this' and asks me for the assembler code for strcat.
  • stoberstober Member Posts: 9,765 ✭✭✭
    [b][red]This message was edited by stober at 2006-4-6 18:52:15[/red][/b][hr]
    Learn to use your compiler's debugger -- VC++ has one of the best debuggers in the industry and makes your life a lot easier.

    use strtok() to parse out the tokens from the string. strtok() will change the original string, so you need to duplicate it for strtok(). Your version of this function was overwriting the buffers causing memory crash.
    [code]
    void seperateWords(char string[26], char wordArray[3][20])
    {
    char tmp[26];
    int n = 0;
    strcpy(tmp,string);
    char*ptr = strtok(tmp," ");
    memset(wordArray,0,sizeof(wordArray));
    while(ptr)
    {
    strcpy(wordArray[n++],ptr);
    ptr = strtok(0," ");
    }
    }
    [/code]



Sign In or Register to comment.