How to fix 'struct does not refer to a value' - c++

in my project there are 2 structures, one of which relates to a binary tree, the second to students data
in ' int main ' I can’t access the add function GETDATA
' 'ZKR' does not refer to a value . ' (xcode)
I also can’t create a counter of the number of vertices of my tree
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
struct ZKR {
char FACULTY[30];
int ACADEMIC_DEGREE;
char FIO[30];
};
struct point
{
char *data;
point *left;
point *right;
};
point* tree(int n, point* p)
{
point *r;
int nl, nr;
if (n == 0) { p = NULL; return p; }
nl = n / 2;
nr = n - nl - 1;
r = new point;
char s[50];
cout << "Значение: ";
cin >> s;
r->data = new char[strlen(s) + 1];
strcpy(r->data,s);
r->left = tree(nl, r->left);
r->right = tree(nr, r->right);
p = r;
return p;
}
void GETDATA(ZKR*M, int N)
{
cin.ignore();
for (int i = 0; i < N; i++)
{
cout << "\n";
cout << "FACULTY: ";
cin.getline(M[i].FACULTY, 30);
cout << "\n";
cout << "FIO: ";
cin.getline(M[i].FIO, 30);
cout << "\n";
cout << "ACADEMIC DEGREE: ";
cin >> M[i].ACADEMIC_DEGREE;
cin.ignore();
}
}
void treeprint(point *p, int &count) {
if (p != NULL) {
treeprint(p->left, count);
cout << p->data << " ";
treeprint(p->right, count);
if ((p->left == NULL) && (p->right == NULL))
count = count + 1;
}
}
int main()
{
setlocale(LC_ALL, "russian");
srand(time(NULL));
int n = 0, k = 0, count = 0;
point *beg = nullptr;
cout << "Enter the number of students" << endl;
int N;
cin >> N;
ZKR*M = new ZKR[N];
do
{
cout << "1. BUILD a binary tree\n";
cout << "2. SHOW a binary tree\n";
cout << "3. GETDATA\n";
cin >> k;
switch (k)
{
case 1:
cout << "Введите количество элементов" << endl;
cin >> n;
beg = tree(n, beg);
cout << endl;
break;
case 2:
treeprint(beg, count);
cout << endl;
cout << "Листьев в дереве: " << count << endl;
break;
case 3:
GETDATA(ZKR*M, N);
break;
}
} while (k != 4);
system("pause");
return 0;
}
I will be grateful for any help

This line is not syntactically correct:
GETDATA(ZKR*M, N);
Replace it with:
GETDATA(M, N);
M is a pointer to an object of type ZKR.

Related

C++ My files were working before I attempted to move them into a source file, and now there are no errors and nothing being displayed

My apologies for being a little vague about the issue(s) in the topic, but I really am not sure. I am attempting to create a course_directory as a lab project for school, and up until now everything had been progressing quite well. I have written and tested each of the function in the main.cpp before attempting to move the class functions into the .hpp file. My goal is to have nothing in main except for a call to "Run" that will open a file, and in turn make a call to "displayMenu", and allow the user to interact with the information as they choose, but now everything is compiling correctly, and nothing is being displayed.
Course_directory.hpp
#include "Course_Directory.h"
using namespace std;
Course_Directory::Course_Directory(){};
Course_Directory courses[1001];
void Course_Directory::displayMenu(){
cout << "1.Print all courses" << endl;
cout << "2.Print all courses for a department" << endl;
cout << "3.Print roster for a course" << endl;
cout << "4.Print the largest class" << endl;
cout << "5.Swap two classes" << endl;
cout << "6.Print schedule for a student" << endl;
cout << "7.Exit" << endl;
char dept[50], dept2[50];
int courseNo, courseNo2, i, choice;
while (choice != 7) {
cout << "\nEnter your choice: ";
cin >> choice;
if (choice == 1)
printAllCourses(i);
else if (choice == 2) {
cout << "\nEnter department name:";
cin >> dept;
for(int j = 0; j < sizeof(dept); j++)
{
dept[j] = (toupper(dept[j]));
}
coursesInDept(dept, i);
}
else if (choice == 3) {
cout << "\nEnter course number:";
cin >> courseNo;
studentsInCourse(courseNo, i);
}
else if (choice == 4) {
largestClass(i);
}
else if (choice == 5) {
cout << "\nEnter first department name :";
cin >> dept;
for(int j = 0; j < sizeof(dept); j++)
{
dept[j] = (toupper(dept[j]));
}
cout << "\nEnter first course number: ";
cin >> courseNo;
cout << "\nEnter second department name :";
cin >> dept2;
for(int k = 0; k < sizeof(dept2); k++)
{
dept2[k] = (toupper(dept2[k]));
}
cout << "\nEnter second course number: ";
cin >> courseNo2;
swap2(dept, courseNo, dept2, courseNo2, i);
}
else if (choice == 6) {
int id;
cout << "\nEnter a student Id:";
cin >> id;
schedule(id, i);
}
}
cout << "Goodbye!\n";
}
void Course_Directory::printAllCourses(int len){
for (int i = 0; i < len; i++)
cout << "Course name: " << courses[i].courseName << ", Course
number: " << courses[i].courseNum << endl;
cout << len;
}
void Course_Directory::coursesInDept(char *dept, int len) {
for (int i = 0; i < len; i++)
if (strcmp(dept, courses[i].courseName) == 0)
cout << "Course Name: " << courses[i].courseName << ",
Course number: " << courses[i].courseNum << endl;
}
void Course_Directory::studentsInCourse(int courseNo, int len) {
for (int i = 0; i < len; i++)
if (courseNo == courses[i].courseNum) {
for (int m = 0; m < courses[i].numStudents - 1; m++)
cout << courses[i].IDs[m] << ",";
cout << courses[i].IDs[courses[i].numStudents - 1] <<
endl;
}
}
void Course_Directory::largestClass(int len) {
int max = -999;
for (int i = 0; i < len; i++) {
if (courses[i].numStudents > max)
max = courses[i].numStudents;
}
for (int i = 0; i < len; i++) {
if (courses[i].numStudents == max){
cout << "\nThe largest class is in department: " <<
courses[i].courseName
<< ", and the course number is: " << courses[i].courseNum
<< "\n";
cout << "The class currently has " << max << " students
enrolled.\n";
}
}
}
void Course_Directory::swap2(char *firstDep, int firstNo, char
*secondDep, int secondNo, int len) {
Course_Directory temp;
int firstIndex, secondIndex;
for (int i = 0; i < len; i++) {
if (strcmp(firstDep, courses[i].courseName) == 0 &&
courses[i].courseNum == firstNo)
firstIndex = i;
if (strcmp(secondDep, courses[i].courseName) == 0 &&
courses[i].courseNum == secondNo)
secondIndex = i;
}
temp = courses[firstIndex];
courses[firstIndex] = courses[secondIndex];
courses[secondIndex] = temp;
}
void Course_Directory::schedule(int id, int len) {
cout << "Courses student " << id << " is enrolled in: " << endl;
for (int i = 0; i < len; i++) {
for (int j = 0; j < courses[i].numStudents; j++)
if (courses[i].IDs[j] == id)
cout << courses[i].courseNum << " \n";
}
}
Course_Directory.h
#ifndef COURSE_DIRECTORY_H
#define COURSE_DIRECTORY_H
#include <iostream>
using namespace std;
class Course_Directory{
private:
int* deptSize;
string filename;
public:
char courseName[1001];
int courseNum;
int numStudents;
int IDs[1001];
int* choice;
void displayMenu();
Course_Directory();
//Course_Directory(const Course_Directory& original);
//~Course_Directory();
//void run(string);
void coursesInDept(char*, int);
void studentsInCourse(int, int);
void largestClass(int);
void swap2(char*, int, char*, int, int);
void schedule(int, int);
void printAllCourses(int);
};
#include "Course_Directory.hpp"
#endif //COURSE_DIRECTORY_H
main.cpp
#include "Course_Directory.h"
using namespace std;
class Course_Directory;
int main() {
ifstream file;
file.open("input.txt");
char *token;
int i = 0, j;
Course_Directory courses[100];
if (!file.fail()) {
std::string line;
while (getline(file, line)) {
j = 0;
char *str = const_cast<char *>(line.c_str());
token = strtok(str, " ");
while (token != NULL)
{
if (j == 0)
strcpy(courses[i].courseName, token);
else if (j == 1)
courses[i].courseNum = atoi(token);
else if (j == 2)
courses[i].numStudents = atoi(token);
else
courses[i].IDs[j - 3] = atoi(token);
j++;
token = strtok(NULL, " ");
cout << courses[i].courseName << endl;
}
i++;
}
file.close();
}
else{
cout << "File not found." << endl;
}
//menu
Course_Directory displayMenu();
return 0;
}
If I leave Course_Directory off of displayMenu(); then I get "displayMenu" was not declared in this scope. I'm not sure what the problem is or why the menu will not display. Any help would be greatly appreciated!

Represent Matrix in tabular form C++

Currently, I am trying to use the linked node to represent the matrix. My codes are working fine, while I not sure it is possible to represent my matrix in tabular form instead of (x,y) = value.
While my printout() method only print it out when temp != NULL, and this is the result
Element position(3,3) = 9
I want to represent it like
1 2 3
4 5 6
7 8 9
Below is my codes with the linked node in the matrix
#include <iostream>
#include <conio.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
typedef struct node
{
int column;
int value;
int row;
struct node *next;
} element;
void Init(element *x[])
{
int i;
for (i = 0; i < 11; i++) {
x[i] = NULL;
}
}
void Insert(element *x[], int row, int column, int value)
{
int r = row;
element *p;
element *news = (element*)malloc(sizeof(element));
news->row = row;
news->column = column;
news->value = value;
if (x[r] == NULL)
{
x[r] = news;
news->next = NULL;
}
else
{
p = x[r];
if (news->column < p->column)
{
news->next = p;
x[r] = news;
}
else if (news->column > p->column)
{
while (p->next != NULL && p->next->column < news->column)
{
p = p->next;
}
news->next = p->next;
p->next = news;
}
else cout << "An element already exists there!!\n";
}
}
void Printout(element *x[])
{
int i, test = 0;
element *temp;
for (i = 0; i < 11; i++) {
temp = x[i];
while (temp != NULL) {
cout << "Element position" << "(" << i << "," << temp->column << ") = " << temp->value << endl;
test = 1;
temp = temp->next;
}
}
if (test == 0) {
cout << "This matrix is empty" << endl;
}
}
int main(int argc, const char * argv[]) {
int choice, column, row, value, number;
element *a[10], *b[10], *sum[10];
Init(a); Init(b); Init(sum);
do
{
cout << "Add Sparse Matrix" << endl;
cout << "1. Insert in A" << endl;
cout << "2. Insert in B" << endl;
cout << "3. Print 2 matrix" << endl;
cout << "0. Exit" << endl;
cout << "Please enter your option" << endl;
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter row -> ";
cin >> row;
} while (row < 0 || row > 11);
do
{
cout << "Enter column -> ";
cin >> column;
} while (column < 0);
cout << "Enter value -> ";
cin >> value;
Insert(a, row, column, value);
break;
case 2:
do
{
cout << "Enter row -> ";
cin >> row;
} while (row < 0 || row > 11);
do
{
cout << "Enter column -> ";
cin >> column;
} while (column < 0);
cout << "Enter value -> ";
cin >> value;
Insert(b, row, column, value);
break;
case 3:
cout << "\n::::::: MATRIX A :> \n\n";
Printout(a);
cout << "\n::::::: MATRIX B :> \n\n";
Printout(b);
break;
default:
cout << "WRONG CHOICE\n\n";
}
} while (choice != 0);
return 0;
}
Sorry for asking this question, I am just started to learn C++ programming, need someone to enlighten me. Thank you for your concern.
Since your linked list is used to saves the memory usage, you can write some codes to print the zero until the width is reached.
void SM::Printout(element *x[])
{
int width = -1;
for (int row = 0; row < 11; row++)
{
for (element *node = x[row]; node != NULL; node = node->next)
if (node->column > width)
width = node->column;
}
width++;
for (int row = 0; row < 11; row++)
{
int col = 0;
for (element *node = x[row]; node != NULL; node = node->next)
{
for (; col < node->column; col++)
cout << "0 ";
if (node->value != NULL)
cout << node->value << " ";
col = node->column + 1;
}
for (; col < width; col++)
cout << "0 ";
cout << "\n";
}
}

Segmentation fault c++ Hexadecimal Calculator

I keep getting a segmentation fault when I attempt to add a hex number to the current hex number. Here is the line where I'm getting my segmentation fault(I think):
newHex->insertTail(sum);
Here is the whole program:
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <string>
using namespace std;
#undef NULL
const int NULL = 0;
const char SENTINEL = '#';
typedef int element;
class listnode {
public:
element data;
listnode * next;
};
class LList {
private:
listnode * head;
listnode * tail;
listnode * view;
public:
LList();
~LList();
void read();
listnode* next();
void print();
void insertTail(element val);
void clean();
//void add(LList operand);
//void multiply(LList operand);
element deleteHead();
};
class Calculator {
public:
Calculator();
LList* add(LList& left, LList& right);
//LList* multiply(LList& left, LList& right);
};
Calculator::Calculator() {
};
LList* Calculator::add(LList& left, LList& right) {
int sum, carry = 0, lval = 0, rval = 0;
bool calculating = true;
listnode *leftNode;
listnode *rightNode;
LList* newHex;
while(calculating) {
leftNode = left.next();
rightNode = right.next();
if(leftNode == NULL) {
lval = 0;
}
else
lval = leftNode->data;
if(rightNode == NULL) {
rval = 0;
}
else
rval = rightNode->data;
if(leftNode == NULL && rightNode == NULL) {
calculating = false;
if(carry != 0) {
newHex->insertTail(carry);
}
break;
}
sum = lval + rval + carry;
carry = 0;
if(sum >= 16) {
carry = 1;
sum -= 16;
}
cout << "Segmentation Fault Below :)" << endl;
newHex->insertTail(sum);
}
return newHex;
};
listnode* LList::next() {
listnode* temp = view;
if(temp != NULL)
view = view->next;
if(view == NULL) {
}
return temp;
};
LList::LList(){
head = NULL;
view = NULL;
};
void LList::print() {
listnode * temp;
int i = 0;
string printValues;
temp = head;
while(temp != NULL) {
int var = temp -> data;
char character = ' ';
if(i % 3 == 0 && i != 0)
printValues += ',';
i++;
if(var > 9 && var < 16)
{
character = static_cast <char>(var + 65 - 10);
}else if (var <= 9 && var >= 0)
character = static_cast <char>(var + 48);
printValues += character;
temp = temp -> next;
}
string tempValues;
for(int i = printValues.length() - 1; i >= 0; i--)
tempValues += printValues[i];
cout << tempValues;
cout << endl;
};
void LList::read() {
string userval;
int i;
bool parsing = true;
char curval;
vector <int> values;
clean();
while(parsing) {
cin >> userval;
for(unsigned int i = 0; i < userval.length(); i++) {
curval = userval[i]; //this is your character
if(curval >= 48 && curval <= 57)
values.push_back(static_cast <int>(curval -
48));
if(curval >= 65 && curval <= 70)
values.push_back(static_cast <int>(curval -
65 + 10));
if(curval == ' ')
break;
if(curval == SENTINEL) {
parsing = false;
break;
}
}
}
for(int i = values.size() -1; i >= 0; i--) {
insertTail(values[i]);
}
};
void LList::insertTail(element val) {
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = NULL;
if(head == NULL) {
head = temp;
view = head;
}
else
tail -> next = temp;
tail = temp;
};
void LList::clean() {
while(head != NULL)
deleteHead();
};
void validCommands() {
cout << "Valid commands are:" << endl;
cout << " e enter enter the current ";
cout << "hexadecimal ";
cout << "number from the keyboard" << endl;
cout << " a add add a new hexadecimal ";
cout << "number to the current hex. number" << endl;
cout << " m multiply ";
cout << "multiply a new hexadecimal number ";
cout << "by the current hex. number" << endl;
cout << " h help show this help menu" << endl;
cout << " q quit quit the program" << endl << endl;
};
/*
void LList::multiply(LList operand) {
int left, right, product, carry;
listnode* operandNext = operand.next();
listnode* currentNode = this->head;
while(operandNext != NULL) {
left = operandNext->data;
right = currentNode->data;
product = left * right;
carry = 0;
if(product >= 16) {
product -= 16;
carry = 1;
}
if(currentNode == NULL) {
this->insertTail(left);
}
currentNode->data = product;
currentNode = currentNode->next;
operandNext = operandNext->next;
}
};
*/
element LList::deleteHead() {
listnode * temp;
temp = head;
head = head -> next;
delete temp;
return temp -> data;
};
LList::~LList(){
delete head;
};
int main() {
LList L, add,multiply;
Calculator calc;
L.insertTail(0);
char option;
bool run;
cout << "Hexadecimal Calculator, Ver 1.0.0 \n";
cout << endl;
do {
cout << "Current Hexadecimal number is: ";
L.print();
cout << endl;
cout << "Command (h for help): ";
cin >> option;
cout << endl << endl;
switch(option) {
case 'e' :
cout << "Enter a hexadecimal number ";
cout << "followed by #: ";
L.read();
cout << endl << "Entering completed.";
cout << endl << endl;
break;
case 'a' :
cout << "Adding a new hexadecimal number ";
cout << "to the current hex. number" << endl;
cout << endl;
cout << "Enter a hexadecimal ";
cout << "number, follow by #: ";
add.read();
cout << endl << "Addition completed.";
cout << endl;
L = *calc.add(L, add);
cout << endl;
break;
case 'm' :
cout << "Multiplying a new hexadecimal ";
cout << "number ";
cout << "to the current hex. number" << endl;
cout << endl;
cout << "Enter a hexadecimal ";
cout << "number, follow by #: ";
//multiply.read();
cout << endl << "Multiplication completed.";
cout << endl;
//L.multiply(multiply);
cout << endl;
break;
case 'h' : validCommands();
break;
case 'q' : run = false;
break;
};
} while (run);
exit(0);
}
If I'm not wrong, the problem is newHex, in Calculator::add()
You define a pointer to a LList
LList* newHex;
and you use it
newHex->insertTail(carry);
[...]
newHex->insertTail(sum);
without allocate it.

Heap Corruption Detected (Simulating Dynamic Array with Static Array)

I wanna simulate dynamic array by using static array. However, when I delete some elements from the array and then try to add the array, I got an error saying 'Heap Corruption Detected'.
Here's my code:
#include <iostream>
#include <algorithm>
using namespace std;
int INIT_SIZE = 5;
int MAX_SIZE = INIT_SIZE;
void printArray(int *arr, int n){
cout << "n= " << n << " >> [";
for(int i = 0; i < n ;i++){
cout << arr[i];
if ( i < n-1)
cout << ",";
}
cout << "]\b\n";
}
bool deleteEl(int *&arr, int &n, int el){
int *newArr;
bool found = false;
int cnt = 0;
if( n == 0){
return false;
}
else{
for(int i = 0; i < n;i++){
if( arr[i] == el){
found = true;
cnt++;
}
}
if( !found){
return false;
}
newArr = new int[n-cnt];
int k = 0;
for(int i = 0; i < n ; i++){
if( arr[i] != el){
newArr[k++] = arr[i];
}
}
n -= cnt;
if (n <= (MAX_SIZE / 2) && (MAX_SIZE / 2) >= INIT_SIZE){
MAX_SIZE /= 2;
}
delete[] arr;
arr = newArr;
}
return true;
}
bool addEl(int *&arr, int &n, int el){
int *newArr;
if( n >= MAX_SIZE){
newArr = new int[2 * MAX_SIZE];
//std::copy(arr,arr+MAX_SIZE, newArr);
for(int i = 0 ; i < n;i++){
newArr[i] = arr[i];
}
n++;
newArr[n-1] = el;
MAX_SIZE *= 2;
delete[] arr;
arr = newArr;
}else{
arr[n++] = el;
}
return true;
}
int main(int argc, char *argv[]){
int *arr = new int[MAX_SIZE];
int n = 0;
int input = 0;
do{
cout << "1. Add Element : " << endl;
cout << "2. Del Element : " << endl;
cout << "3. Exit : " << endl;
cin >> input;
if( input == 1){
cout << "Enter new element : " << endl;
int newEl;
cin >> newEl;
if( addEl(arr, n, newEl)){
cout << "New Element Added. " << endl;
}else{
cout << "Add new element failed. " << endl;
}
}else if (input == 2){
cout << "Enter deleted element : " << endl;
int del;
cin >> del;
if( deleteEl(arr, n, del)){
cout << "Deletion OK" << endl;
}else{
cout << "Deletion Fail. Data Not found." << endl;
}
}else if(input == 3){
cout << "Program exiting ..." << endl;
}else{
cout << "Input is wrong. Enter correct input (1,2, or 3)." << endl;
}
printArray(arr,n);
}while(input != 3);
delete [] arr;
return 0;
}
Have a look at the Hinnant's stack allocator and consider using STL instead of naked arrays.

C++ Programming Errors: C4703, C4700

i am relatively new to programming, i just learned c++ and i am getting 2 errors for a HW assignment from school;
Error 2 error C4700: uninitialized local variable 'searchValueptr' used Line 83
and
Error 3 error C4703: potentially uninitialized local pointer variable 'createdArray' used
Line 70
I cannot for the life of me figure out why or how to fix it can some one help me please.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// prototypes
int *createArray(int &size);
void findStats(int *arr, int size, int &min, int &max, double &average);
int *searchElement(int *arr, int size, int *element);
int main ()
{
int choice, size;
bool menuOn = true;
while (menuOn == true)
{
cout << "1 - Create and initialize a dynamic array.\n";
cout << "2 - Display statistics on the array.\n";
cout << "3 - Search for an element.\n";
cout << "4 - Exit.\n";
cout << "Enter your choice and press return: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
int *createdArray;
cout << "Please enter a size for the array: ";
cin >> size;
createdArray = createArray(size);
for (int x=0; x < size; x++)
{
cout << "Value of array at index " << x << ": " << createdArray[x] << endl;
}
cout << endl;
break;
case 2:
int minNum;
int maxNum;
double avgNum;
findStats(createdArray, size, minNum, maxNum, avgNum);
cout << endl << "The maximum number is: " << maxNum;
cout << endl << "The minimum number is: " << minNum;
cout << endl << "The average number is: " << avgNum;
cout << endl << endl;
break;
case 3:
int *searchValueptr;
int searchValue;
cout << "Enter a value to search for: ";
cin >> searchValue;
*searchValueptr = searchValue;
searchElement(createdArray, size, searchValueptr);
break;
case 4:
cout << "Thanks for using this program";
menuOn = false;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}
}
cout << endl;
system("pause");
return 0;
} // end function main ()
int *createArray(int &size)
{
unsigned seed = time(0);
srand(seed);
int *randArray = new int [size];
for (int x=0; x < size; x++)
{
randArray[x] = rand();
}
cout << endl;
return randArray;
}
void findStats(int *arr, int size, int &min, int &max, double &average)
{
min = arr[0];
max = arr[0];
double sum = 0;
for (int count = 0; count < size; count++)
{
if (min >= arr[count])
{
min = arr[count];
}
if (max <= arr[count])
{
max = arr[count];
}
sum += arr[count];
}
average = sum/size;
}
int *searchElement(int *arr, int size, int *element)
{
bool match = false;
for (int count = 0; count < size; count++)
{
if (arr[count] == *element)
{
match = true;
}
}
if (match)
{
cout << "Match Found at: " << element;
return element;
}
else
{
cout << "No Found";
return 0;
}
}
either use
searchValueptr = &searchValue;
or send the pass the address of searchValue
searchElement(createdArray, size, &searchValue);
break;