Jump to content

C++ Programming Issue

gwtx2
Go to solution Solved by Slottr,
9 minutes ago, gwtx2 said:

Slottr, What's your suggestion to not setting it to -1?

I was referencing your array comment. Starting an array at 1 where arrays naturally begin at 0 will cause issue and redundant memory use

New to this forum so I hop I post correctly.

//My problem is, when after I call the function, alpha somehow gets changed to the last word in my array

#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;

//function declaraion
int readPuzzles (string [], int&);


//main
int main(){

const int MAX_WORDS=20;
const int MAX_WORD_SIZE=11;
const int MAX_GUESSES=26;
const int NUM_LETTERS=26;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string words[MAX_WORDS];
int wordNum= MAX_WORDS, letters=0, misses=0, numWords=0;
char guess;
bool guessed[MAX_WORD_SIZE];

//call function
readPuzzles(words , numWords);


cout<<"I'm back from function call. I found "<<numWords<<" words."<<endl;


for ( int i=1; i<numWords+1; i++){
        cout<<i<<":"<<words[i]<<endl;
    }

//***alpha somehow becomes the last word in my text file (which is a file with 20 words, one word per line)
cout<<"alpha=: "<<alpha<<endl;

return 0;
}


//FUNCTION
int readPuzzles (string words [], int& numWords){
ifstream inFile;
string filetxt;

inFile.open("puzzles.txt");


if (inFile.is_open())
{
while (getline (inFile,filetxt)){

    numWords++;
    words[numWords] = filetxt;
}

inFile.close();
return numWords;

} else {
    cout<<"Error Opening File!"<<endl;
    inFile.close();
    return numWords=0;
}

}

 

puzzles.txt

Link to comment
Share on other sites

Link to post
Share on other sites

It's a buffer overflow. When your function tries to write to words[20], it is actually writing to the memory address that contains the value of alpha, so the value of alpha is overwritten. Change the declaration of words to words[MAX_WORDS + 1].

 

Edit: My original explanation is incorrect. words is not a std::string, but an array of std::string, which is zero index. It is still a buffer overflow, because of these two lines:

 

numWords++;
words[numWords] = filetxt;

numWords needs to be incremented after each line is read, so these two lines need to be switched.

Link to comment
Share on other sites

Link to post
Share on other sites

Hey badreg, that works! I typically like to start my arrays at 1 (using another programming language). I also tried setting numWords to -1 initially so that the first word is numWords(0) and that also works. Thanks for the tip. Is that the only option available to fix this, you think?

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, gwtx2 said:

Hey badreg, that works! I typically like to start my arrays at 1 (using another programming language). I also tried setting numWords to -1 initially so that the first word is numWords(0) and that also works. Thanks for the tip. Is that the only option available to fix this, you think?

That would be bad practice. You'll end up confusing yourself and messing up with memory allocation more often than not.

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

Slottr, What's your suggestion to not setting it to -1?

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, gwtx2 said:

Slottr, What's your suggestion to not setting it to -1?

I was referencing your array comment. Starting an array at 1 where arrays naturally begin at 0 will cause issue and redundant memory use

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, gwtx2 said:

Slottr, What's your suggestion to not setting it to -1?

Most programming languages (if not all) index arrays starting at position 0. Starting an array at anything other than 0 is confusing anyone else who reads your code because that is not the paradigm that's set by the rest of the programmers universe. This makes sense when you think about it as well because decimal doesn't start at 1 it starts at 0 along with every other numeral system.  Its also very good practice for the simple fact that computers can use different number systems depending on the application such as binary, octal, decimal and hexadecimal.

 

Binary (which is base 2) is 0 and 1,

...

..

Octal (base 8 ) 0, 1, 2, 3, 4, 5, 6, 7,

Decimal ( Base 10 which is our every day number system) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

...

..

Hexadecimal (base 16)  0 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F,      Which converted to decimal is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, trag1c said:

Most programming languages (if not all) index arrays starting at position 0.

tmatlab, erlang, fortran and a bunch of others

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks all for the help. It was very helpful!!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×