Unscrambling words in C++ using pointers - c++

for my csc 102 class I have to create a program that will replace certain letters with others in a text file. The file holds five words and I have to use a class system. This sounds simple enough to me, but my variables in the class have to be pointer variables. Being that all of my classes are online now, trying to use my professors handwritten notes on canvas is all I have to explain how he wants projects done. I sort of understand what a pointer is. The pointer holds the address of a value. You have to dereference it with * to get the value stored there. However, I just don't see how pointers are to be used in this problem. This is the first prompt out of four and I just don't get what i'm supposed to do with the pointers. I will attach my code below and what the prompt asks me to do. I appreciate any help.
Prompt:
Problem 1: A computer keyboard has defect (like speech defect in humans) in reading for ‘p’ /’P’ as ‘f’/’F’ or vice versa and ‘n’/’N’ as ‘l’/’L’ or vice versa
Create a class with pointer variables that would correct the spelling of each word from the input.txt print them in in an output.txt.
Class Header file:
#pragma once
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class keyfix
{
char* input;
char* output;
string* word;
char* wordarray[10];
public:
keyfix();
~keyfix();
void readfile(ifstream&);
void correctspelling(ofstream&);
void outputfile();
void print()const;
};
class implementation file:
#include "keyfix.h"
keyfix::keyfix() {
input = new char;
output = new char;
*input = 'o';
*output = 'o';
word = new string;
*word = "empty";
*wordarray = new char;
}
keyfix::~keyfix() {
delete input;
delete output;
delete word;
input = output = NULL;
word = NULL;
}
void keyfix::readfile(ifstream& a) {
for (int i = 0; i < 10; i++) {
a >> wordarray[i];
}
}
I have an ifstream object and an ofstream object in main.
In my mind, I planned to use a character array instead of a string so that I could change each incorrect letter without using functions we haven't yet covered like anything in <algorithms>. I am in real need of some direction here. I am sure some of y'all can look at my code and tell, but I get a nullptr error whenever I try to test the read file function.
EDIT: I am now writing it with wordarray as a plain char array. It may be accepted this way, it may not be.
Also, the size is 10 because the file is provided and the largest word only has 10 letters.

As recommended in the comments, I wrote the program without pointers, and then changed a variable to a pointer type in my class. Here is the final project for any future students who happen to have this exact problem. (I saw the problem on chegg so I know he uses it commonly)
#pragma once
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class keyfix
{
char* input;
char wordarray[10];
public:
keyfix();
~keyfix();
void readfile(ifstream&);
void correctspelling(ofstream&);
void print()const;
};
#include "keyfix.h"
keyfix::keyfix() {
input = new char;
*input = 'o';
wordarray[10];
}
keyfix::~keyfix() {
delete input;
input = NULL;
}
void keyfix::readfile(ifstream& a) {
for (int i = 0; i < 10; i++) {
wordarray[i] = NULL;
}
a >> wordarray;
}
void keyfix::correctspelling(ofstream& b) {
for (int index = 0; index < 10; index++) {
*input = wordarray[index];
if (*input == 'f') {
wordarray[index] = 'p';
}
else if (*input == 'F') {
wordarray[index] = 'P';
}
else if (*input == 'p') {
wordarray[index] = 'f';
}
else if (*input == 'P') {
wordarray[index] = 'F';
}
else if (*input == 'n') {
wordarray[index] = 'l';
}
else if (*input == 'N') {
wordarray[index] = 'L';
}
else if (*input == 'l') {
wordarray[index] = 'n';
}
else if (*input == 'L') {
wordarray[index] = 'N';
}
b << wordarray[index];
}
b << endl;
}
void keyfix::print()const {
for (int i = 0; i < 10; i++) {
cout << wordarray[i];
}
cout << endl;
}
#include"keyfix.h"
//if input is f or F, output p or P
//if input is n or N, output l or L
//if input is p or P, output f or F
//if input is l or L, output n or N
int main() {
ifstream infile;
infile.open("input.txt");
ofstream outfile;
outfile.open("output.txt");
keyfix object;
for (int i = 0; i < 5; i++) {
object.readfile(infile);
object.correctspelling(outfile);
object.print();
}
}

Related

I get a writing access violation of 0x00000... when trying to assign a value into my struct

for part of a school lab I need to read in unique words and their corresponding count with a struct. I am new to structs so please bear with me. I am getting an access violation when I try to write the adress of the current word to the character pointer inside of the current instance of my struct. I have read that this is due to dereferencing a nullptr. I have tried to understand this, but I just don't get it. I have resized arrays just like this on regular char** arrays for accepting new words. I am at a loss, any help would be greatly appreciated. The input file used here is just random words separated by non letter characters but not - or , Here is my code:
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <iomanip>
#include <fstream>
#include <limits>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::right;
using std::left;
using std::ifstream;
using std::ofstream;
const int BUFFER = 100; //I figure this buffer is big enough for any given word
struct Word_Count_STRUCT
{
char* WORD = nullptr;
int COUNT = 0;
};
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//Input for phrase
ifstream iphrase;
//Output to CSV (word count)
ofstream o_count;
//Word Exceptions
ifstream xinWord;
char wordbuffer[BUFFER] = { '\0' };
char ch = 0;
Word_Count_STRUCT** uniquewords = nullptr;
Word_Count_STRUCT** temp = nullptr;
int k = 0;
int wordcount = 0;
char* cword = nullptr; //Current Word
bool NextWord_flag = false;
bool interwordpunct = false;
bool NewWord_flag = true;
iphrase.open("C:\\Users\\me\\Desktop\\henroE.txt");
if (iphrase.is_open())
{
while (!iphrase.eof())
{
iphrase.get(ch);
if (isalpha(ch) || ch == '\'' || ch == '-')
{
wordbuffer[k] = ch;
++k;
NextWord_flag = true;
if (ch == '\'' || ch == '-')
interwordpunct = true;
}
if ( (NextWord_flag == true) && (!isalpha(ch)) && (interwordpunct == false) )
{
k = 0;
cword = new char[strlen(wordbuffer) + 1];
strcpy(cword, wordbuffer);
memset(wordbuffer, '\0', sizeof(wordbuffer));
for (int i = 0; (i < wordcount) && (NewWord_flag == true); ++i)
{
int cmp = _stricmp(uniquewords[i]->WORD, cword);
if (cmp == 0)
{
NewWord_flag = false;
uniquewords[i]->COUNT++;
delete[] cword;
}
}
if (NewWord_flag == true)
{
temp = new Word_Count_STRUCT * [wordcount + 1]();
for (int i = 0; i < wordcount; ++i)
{
temp[i] = uniquewords[i];
}
delete[] uniquewords;
temp[wordcount]->WORD = cword;
temp[wordcount]->COUNT++;
uniquewords = temp;
++wordcount;
NextWord_flag = false;
}
interwordpunct = false;
NewWord_flag = true;
}
}
}
I get an error on this line:
temp[wordcount]->WORD = cword;
I also get an error on the int value COUNT as well if I comment the line above it out. So I am guessing it is something with how I initialized the struct.
Worth noting that if I do not initialize this call:
temp = new Word_Count_STRUCT * [wordcount + 1]();
and instead just leave it as
temp = new Word_Count_STRUCT * [wordcount + 1];
I get another access violation but for reading instead of writing at 0xFFFFF...
At a loss, thank you for any help :)
You've got a number of things wrong. First, using fixed-length character buffers instead of C++ strings is about 20 years out of date and WILL cause buffer overflow errors unless you are exceedingly careful.
But this is an issue:
temp = new Word_Count_STRUCT * [wordcount + 1]();
for (int i = 0; i < wordcount; ++i)
{
temp[i] = uniquewords[i];
}
delete[] uniquewords;
But where did you allocate uniquewords? You declared it.
You also allocate cword outside a loop but the delete it inside a loop -- which seems really fishy, too.
But note that all you've allocated are pointers. I don't see you actually allocating the structure you're trying to put data in.

Pointing to a specific element in a character array

I've been trying to bend my head around this problem for a week now, but I can't seem to find anything online and I've given up on trying to solve it on my own.
My assignment is to write a program which will read names from a file and accept new entries from the user, then sort the entires and write them out to the file. The only crux about this is that I have to sort them in a function and use pointers to do so. This code is supposed to be written in C++ aswell, using character arrays.
The code I have right now looks like this. This is a working version, the only problem is that I don't use neither pointers or a function to sort the names.
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstring>
bool sorted;
using namespace std;
int main()
{
int i = 0;
int numNames = 0;
ifstream ifs;
ifs.open("namn.txt");
char c[20][20];
if(ifs.is_open())
{
while(!ifs.eof())
{
ifs >> c[i];
i++;
}
}
cout<<"How many names do you want to enter?"<<endl;
cin>>numNames;
for(int l = i-1; l<numNames+i-1; l++)
{
system("cls");
cout<<"Enter a name: ";
cin>>c[l];
}
while(sorted == false)
{
for(int j = 0; j<numNames+i-1; j++)
{
for(int k = j; k<numNames+i-1; k++)
{
if(c[j][0] > c[k][0])
{
char snorre[20];
strcpy(snorre,c[j]);
strcpy(c[j],c[k]);
strcpy(c[k],snorre);
}
else if(c[j][0] == c[k][0])
{
if(c[j][1] > c[k][1])
{
char snorre[20];
strcpy(snorre,c[j]);
strcpy(c[j],c[k]);
strcpy(c[k],snorre);
}
}
}
}
cout<<endl<<endl<<endl;
ofstream ofs;
ofs.open("namn.txt");
for(int o = 0; o<numNames+i-1; o++)
{
cout<<c[o]<<" ";
ofs<<c[o]<<endl;
}
ofs.close();
system("pause");
sorted = true;
}
}
So hopefully someone could help me out with this problem, thanks in advance! :)
To get your code to use pointers and functions, you can do this- you should change your code and make it use the following:
First, Get each name from the file to an std::string, using getline(ifstream_object, std::string_object), for reference see here.
Convert each one to a const char * (also shown in that example), using .c_str().
Do the following to each of the new names entered.
Store all names entered in this array pointers: char *names[20];, like this: names[i] = name;
Next, Create a function such as follows:
int location_of_bigger_string(const char* s1, const char* s2)
{
// Returns 1 if s1 should be before s2 and 2 otherwise
// This way, you use functions and pointers together.
// Use strcmp(s1,s2) here to determine returning value
}
strcmp(char*, char*) - read about it here.
Finally, to sort all the strings, use qsort or this example.
Here's the complete code,
Note that the compare function gets pointers to the elements, here the elements are pointers themselves, so what's passed to "compare" function is of type "char **"
{
#include "stdafx.h"
#include<iostream>
//retruns +1 if str1 > str2 alphabetically
int compare(const void * a, const void * b )
{
const char * str1 = *((const char **)a);
const char * str2 = *((const char **)b);
int i;
for ( i = 0 ; str1[i] && str2[i] ; i++ )
{
if ( str1[i] > str2[i] )
{
return +1;
}
else if ( str1[i] < str2[i] )
{
return -1;
}
}
//one or both strings have ended
if (str1[i]) //str1 is longer
return +1;
else if (str2[i]) //str2 is longer
return -1;
else
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
char * names[]={"Zebra","Kousha","Koosha","Kou","Koush","Test"};
qsort( names, 6, sizeof(char *), compare );
return 0;
}
}

Replacing character in string in cpp using pointer

I am very new new to cpp and trying to replace second occurrence of '*' with '!' char in a given string using following method.
#include <iostream>
#include <string.h>
using namespace std;
void replaceChar(char **inp){
char *tmp = *inp;
const char *c = "*";
char *cmark = strstr(tmp,c);
cout<< *cmark;
if(cmark != NULL && strlen(cmark) > 1){
cmark++;
if(strstr(cmark,c)){
int len = strlen(cmark);
cout<<"len"<<len;
for(int i=0;i<len;i++){
if(cmark[i] == '*'){
cout<<"i.."<<i;
cmark[i] = '!';//error point
}
}
}
}
}
int main() {
char * val = "this is string*replace next * with ! and print";
replaceChar(&val);
cout<<"val is "<< val;
return 0;
}
I am getting run time error on error point line.If I comment out this line I am getting correct index of '*' to be replaced.
Is it possible to replace '*' with '!' using cmark[i] = '!'?
Check this difference between char s[] and char *s in C
#include <iostream>
#include <string.h>
using namespace std;
void replaceChar(char *inp){
char *tmp = inp;
const char *c = "*";
char *cmark = strstr(tmp,c);
cout<< *cmark;
if(cmark != NULL && strlen(cmark) > 1){
cmark++;
if(strstr(cmark,c)){
int len = strlen(cmark);
cout<<"len"<<len;
for(int i=0;i<len;i++){
if(cmark[i] == '*'){
cout<<"i.."<<i;
cmark[i] = '!';
}
}
}
}
}
int main() {
char val[] = "this is string*replace next * with ! and print";
replaceChar(val);
cout<<"val is "<< val;
return 0;
}
There is no need to pass pointer to pointer in method.
Instead you can just pass original pointer to string.
You can do it in much simpler way.
void replaceChar(char *inp){
int i;
int second = 0;
/* Strings in C\C++ is null-terminated so we use it to determine
end of string */
for (i = 0; inp[i] != '\0'; ++i) {
if (inp[i] == '*') {
/* Use flag to determine second occurrence of * */
if (!second) {
second = 1;
} else {
inp[i] = '!';
break;
}
}
}
}

Shifting an array of structs C++

I am quite new to c++ programming and data structures and really need some help. I am working on an assignment where I have a text file with 100 lines and on each line there is an item, a status(for sale or wanted), and a price. I need to go through the text file and add lines to an array of structs and as I add lines I need to compare the new information with the previously submitted information. If there is a line that is wanted and has a price higher than a previously input item that is for sale then the item would be removed from the struct and the array of structs shifted.
The place that I am having trouble is in actually shifting all the structs once a line that satisfies the condition is found.
My issue is that when I try to shift the array of structs using the second for loop nothing happens and I just get null structs and nothing seems to move.
Please if you guys can offer any help it would be greatly appreciated.
Below is the code of the text file and my current code.
#include<iostream>
#include<fstream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
struct items
{
string type;
int status;
int price;
} itemArray [100];
int main(int argc, char *argv[]) {
int x = -1;
//int chickenCount = 0;
int counter = 0;
int itemsSold = 0;
int itemsRemoved = 0;
int itemsForSale = 0;
int itemsWanted = 0;
string itemType;
int itemStatus = 0;
int itemPrice = 0;
int match = 0;
ifstream myReadFile( "messageBoard.txt" ) ;
std::string line;
//char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
getline(myReadFile,line); // Saves the line in STRING.
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
//cout<<line<<endl; // Prints our STRING.
x++;
std::string input = line;
std::istringstream ss(input);
std::string token;
while(std::getline(ss, token, ',')) {
counter++;
//std::cout << token << '\n';
if (counter>3){
counter =1;
}
//cout << x << endl;
if (counter == 1){
itemType = token;
//cout<< itemType<<endl;
}
if (counter == 2){
if (token == "forsale"){
itemStatus = 1;
//itemsForSale++;
}
if (token == "wanted"){
itemStatus = 0;
//itemsWanted++;
}
//cout<< itemStatus<<endl;
}
if (counter == 3){
itemPrice = atoi(token.c_str());
//cout<< itemPrice<<endl;
}
//cout<<"yo"<<endl;
}
if (x >= 0){
for (int i = 0; i<100;i++){
if (itemArray[i].type == itemType){
//cout<<itemType<<endl;
if(itemArray[i].status != itemStatus){
if (itemArray[i].status == 1){
if(itemPrice>=itemArray[i].price){
itemsSold++;
match =1;
//itemArray[i].type = "sold";
for (int j=i; j<100-1;j++){
//cout<<j<<endl;
itemArray[j].type = itemArray[j+1].type;
itemArray[j].status = itemArray[j+1].status;
itemArray[j].price = itemArray[j+1].price;
}
i =i-1;
break;
}
}
if (itemArray[i].status == 0){
if(itemArray[i].price>=itemPrice){
itemsSold++;
match = 1;
//itemArray[i].type = "sold";
for (int j=i; j<100-1;j++){
//cout<<j<<endl;
itemArray[j].type = itemArray[j+1].type;
itemArray[j].status = itemArray[j+1].status;
itemArray[j].price = itemArray[j+1].price;
}
i=i-1;
break;
}
}
}
}
}
}
if (counter == 3 && match == 0){
itemArray[(x)].type = itemType;
itemArray[(x)].status = itemStatus;
itemArray[(x)].price = itemPrice;
}
match = 0;
// cout << itemArray[x].type << " " << itemArray[x].status<<" "<<itemArray[x].price<<endl;
}
for(int i=0;i<100;i++){
cout<<itemArray[i].type<< " "<<itemArray[i].status<<" "<<itemArray[i].price<<endl;
}
//cout<<itemArray[1].price<<endl;
cout << itemsSold<<endl;
}
myReadFile.close();
return 0;
}
text file: https://drive.google.com/file/d/0B8O3izVcHJBzem0wMzA3VHoxNk0/view?usp=sharing
Thanks for the help
I see several issues in the code, but without being able to test it, I think the main problem is that you always insert new elements at position 'x' which correspond to the currently line read from the file, without taking into account any shift of elements done. You should insert the new element at the first empty slot (or just overwrite the old element instead of shifting everything).
An other issue is that you do not initialize the status and price in your array.
The best way would be to rewrite the code by using more standard C++ features, for example:
replace the items structure by a class with a constructor defining default values
use object copy (there is no need to copy a struct element by element)
use standard C++ containers like a list (see http://www.cplusplus.com/reference/list/list/) which has insert and erase methods

I want an array of cstrings separated by white space

I am trying to get user input and put it into an array of cstrings separated by a space. When I print the array to the screen though I get nothing. I am sure I am doing something stupid, but I have been stuck trying different ways for a while. Any help would be appreciated. Here is the code.
#include <iostream>
#include <cstring>
using namespace std;
void stuff(char command[][25], int length)
{
char ch;
for(int i = 0; i < length; i ++)
{
int b = 0;
cin.get(ch);
while(!isspace(ch))
{
command[i][b++] = ch;
cin.get(ch);
}
command[i][b] = '\0';
cout << endl;
}
}
int main()
{
char cha[10][25];
char ch;
int len = 0;
while(ch != '\n')
{
cin.get(ch);
if(isspace(ch))
{
len++;
}
}
stuff(cha,len);
for(int i = 0; i < len; i++)
{
cout << cha[i] << endl;
}
cout << len << endl;
return 0;
}
a) ch is undefined when you first test it with while (ch != '\n'). Initialize it to zero or something.
b) You don't write any values into cha in the while loop. Perhaps something like:
int pos = 0;
while(ch != '\n') {
cin.get(ch);
if (isspace((unsigned)ch)) {
if (pos > 0) {
++len;
pos = 0;
}
}
else {
cha[len][pos] = ch;
++pos;
}
}
c) You are reading the stream again in stuff(...) but have already consumed what you wanted to get from the stream in main().
d) This code suffers from a number of buffer overrun and stack corruption opportunities. perhaps use a std::vector<std::string> instead. If it runs out of memory it will throw an exception rather than make a mess on the stack.
Perhaps something like this (untested):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
typedef std::vector<std::string> strvec;
strvec cha;
std::string s;
char ch = 0;
while(ch != '\n') {
cin.get(ch);
if (isspace((unsigned)ch)) {
if (!s.empty()) {
cha.push_back(s);
s.clear();
}
}
else {
s.push_back(ch);
}
}
// don't need to call 'stuff' to null terminate.
for (strvec::iterator i = cha.begin(); i != cha.end(); ++i) {
cout << *i << endl;
}
cout << cha.size() << endl;
}
This could be a little more efficient than it is but hopefully it is easy to understand.
You are reading the whole input in the while cycle in the main to read length(number of strings), but you never store it. Later on in stuff cin.get will read nothing. Maybe store the input in cha during the first cycle?