I'm working on an assignment for school. The code is supposed to read form a file and create an array, then sort the values of the array to output certain info. It works just fine as long as I have 3+ lines of info in the file. If not, I get the following error:
First-chance exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
Unhandled exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
I can't figure out why, any help would be appreciated. Here's the code:
#include <iostream> //calls the information needed
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std; //sets all unmarked commands to std::
const int ARRSIZE = 1000;
struct Student
{
string firstName;
string lastName;
string id, temp;
double gpa;
};
int readArray(ifstream& ifile, Student arr[]);
void swapElements(Student arr[], int i, int j);
void sortArray(Student arr[], int numberInTheArray);
int main()
{ // Declares the needed variables
double sought, min, max;
int i, ival, returnvar, count = 0, mincount, maxcount;
string filename;
ifstream ifile;
Student arr[ARRSIZE];
cout << "Input File Name: ";//requesting the file name
cin >> filename;
ifile.open(filename.c_str());//opening the file
if (!ifile)//checking if it opened or not
{
cout << endl << "That file does not exist!" << endl;//informing the user it did
return 1;//not open and returning 1
}
cout << "Which number do you want to return? ";//requesting the desired number
cin >> ival;
i = ival - 1;
cout << endl;
returnvar = readArray(ifile, arr);
min = arr[0].gpa;
max = arr[0].gpa;
sought = arr[0].gpa;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
if (count == 0)
{
cout << "The file is empty!" << endl;
return 1;
}
cout << "Before Sort:" << endl;
cout << " Min GPA is " << min << " for " << arr[mincount].lastName << "." << endl;
cout << " Max GPA is " << max << " for " << arr[maxcount].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortArray(arr, returnvar);
count = 0;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
cout << "After Sort:" << endl;
cout << " Array[0] GPA is " << min << " for " << arr[0].lastName << "." << endl;
cout << " Array[" << (returnvar - 1) << "] GPA is " << max << " for " << arr[(returnvar - 1)].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
return 0;
}
int readArray(ifstream& ifile, Student arr[])
{
int counter = 0;
while ((ifile) && (counter <= ARRSIZE))
{
ifile >> arr[counter].firstName;
ifile >> arr[counter].lastName;
ifile >> arr[counter].id;
ifile >> arr[counter].gpa;
counter++;
}
return (counter - 1);
}
void sortArray(Student arr[], int numberInTheArray)
{
for (int i = 0 ; i < numberInTheArray - 1; i++)
{
for (int j = 0 ; j < numberInTheArray - 1; j++)
{
if ( arr[j].gpa > arr[j + 1].gpa)
{
swapElements(arr, j, j+1);
}
}
}
}
void swapElements(Student arr[], int i, int j)
{
Student temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Please ignore the insanity and comments. Like I said, for an entry level course.
Try replacing counter <= ARRSIZE with counter < ARRSIZE (a rule of thumb in C is: never use <= in operations related to container sizes).
EDIT: also in your main(), you must check that i < ARRSIZE (equivalently, return error if i >= ARRSIZE). At present you seem to accept the case i == ARRSIZE, which is also wrong. And finally, readArray should return counter (that is, one more than the last written index).
Related
I want to write a program which displays palindromes numbers between 1 and 10000, I wrote a script which displays if the number typed by the user is a palindrome or no but when I add the for loop it gives me false results
My code:
#include<iostream>
using namespace std;
int main()
{
int num, reverse = 0, remainder, temp;
for(num=0;num<1000;num++){
temp = num;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
return 0;
}
You need to make sure that reverse is always 0 at the start of loop iteration:
for(num=0;num<1000;num++){
reverse = 0;
temp = num;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
Managing variables becomes much easier if you declare them where you need them instead of lumping everything together:
int main()
{
for(int num=0;num<1000;num++){
int reverse = 0;
int temp = num;
int remainder = 0;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
return 0;
}
Now you are sure remainder is always initialized at the start of for loop iteration and it cannot live longer than one iteration (for example, it won't live to the next iteration).
I checked your program and noticed a few flaws:
1) All 1-digit numbers are Palindrome because their is reverse is same.
---Your program didn't display them as palindromes.
2) You don't have to display all the details like their remainder and reverse.
---Obviously the Palindromes reverse will be the same as the original. We don't need to display non-palindromes. In some cases reverse was displaying garbage values as well. I don't find a reason that will be of any help to the user.
Solution:-
#include<iostream>
using namespace std;
bool findPalindrome(const int);
int main()
{
for (int i = 0; i < 10000; i++) {
if (findPalindrome(i)) {
cout << "Number " << i << " is a Palindrome!" << endl;
}
}
return EXIT_SUCCESS; // Return EXIT_SUCCESS is my Specialty :D
}
bool findPalindrome(const int num) {
int temp = num;
int reve = 0;
while (temp != 0) {
reve = (reve * 10) + (temp % 10);
temp /= 10;
}
return (reve == num);
}
In addition to the excellent codes other people have posted (I would go with those), this is another (and more readable) way in which you can do the same thing..
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
bool isPalindrome(unsigned number) {
std::vector<unsigned> number_as_vec;
while (number) {
number_as_vec.push_back(number % 10);
number /= 10;
}
for (size_t i = 0, j = number_as_vec.size() - 1; i < j; i++, j--) {
if (number_as_vec.at(i) != number_as_vec.at(j)) {
return false;
}
}
return true;
}
int main(int argc) {
unsigned num = 1;
while (num != 10001) {
if (isPalindrome(num)) {
std::cout << num << "\n";
}
num++;
}
return 0;
}
Also, instead of using vectors, one can simply use a string here:
bool isPalindrome(unsigned number) {
std::string num_as_string(std::to_string(number));
for (size_t i = 0, j = number_as_string.size() - 1; i < j; i++, j--) {
if (number_as_string.at(i) != number_as_string.at(j)) {
return false;
}
}
return true;
}
I've been doing a drill from a book entitled Programming: Principles and Practice Using C++ by Bjarne Stroustrup, and this drill (Chapter 4) primarily deals with vectors. When I run the program, it doesn't give me any output at all. There are no compilation errors though. Here's the code:
#include "std_lib_facilities.h"
int main()
{
vector<double> value;
vector<string> units;
double max_val=-100000, min_val=100000;
double temp, sum=0;
int no_of_inputs=0;
string unit;
int i=0;
cout << "\nEnter the first value: " << endl;
// inputs values and units and assign them in vector value and vector unit respectively.
while (cin >> temp >> unit){
++no_of_inputs;
cout << "Enter the next value: " << endl;
value.push_back(temp);
units.push_back(unit);
}
// converts cm, in and ft to m;
for (i==0; i<units.size(); ++i){
if (units[i]== "cm" || units[i]== " cm"){
value[i] = value[i]/100.0;
}else if (units[i]== "in" || units[i]== " in"){
value[i] = value[i]/2.5/100.0;
}else if (units[i]== "ft" || units[i]== " ft"){
value[i] = value[i]*12/2.5/100.0;
}else if (units[i]=="m" || units[i]== " m"){
}else cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value.";
return 0;
}
// Identifies the max_value and the min_value. Also adds all values.
for (i==0; i<value.size(); ++i){
if (value[i]>max_val){
max_val=value[i];
}
if (value[i]<min_val){
min_val=value[i];
}
sum += value[i];
}
// outputs all values entered (converted to meters)
cout << "\nValues Entered:"<< endl;
sort(value);
for (i==0; i<value.size(); ++i){
cout << value[i] << " meters" << endl;
}
cout << "Total: " << sum << " meters" << endl;
cout << "Smallest value: " << min_val << endl;
cout << "Largest value: " << max_val << endl;
cout << "Total values entered: " << no_of_inputs << endl;
return 0;
}
Can anyone tell me why it is not working?
you have 2 mistakes. The main reason for terminating your program is here
else cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value."; return 0;
you putting return in the first for loop. I guess you may think return is belong to else but because you don't use scope determiner {} only 1 instruction (cout) is belong to else and return is executed every time.
your second mistake is use i==0 instead of i=0 in for loops.
your correct code is this :
vector<double> value;
vector<string> units;
double max_val = -100000, min_val = 100000;
double temp, sum = 0;
int no_of_inputs = 0;
string unit;
int i = 0;
cout << "\nEnter the first value: " << endl;
//inputs values and units and assign them in vector value and vector unit respectively.
while (cin >> temp >> unit){
++no_of_inputs;
cout << "Enter the next value: " << endl;
value.push_back(temp);
units.push_back(unit);
}
//converts cm, in and ft to m;
for (i = 0; i<units.size(); ++i){
if (units[i] == "cm" || units[i] == " cm"){
value[i] = value[i] / 100.0;
}
else if (units[i] == "in" || units[i] == " in"){
value[i] = value[i] / 2.5 / 100.0;
}
else if (units[i] == "ft" || units[i] == " ft"){
value[i] = value[i] * 12 / 2.5 / 100.0;
}
else if (units[i] == "m" || units[i] == " m")
{
}
else
{
cout << "\n\n" << value[i] << " " << units[i] << " is an incorrect input value.";
return 0;
}
}
//Identifies the max_value and the min_value. Also adds all values.
for (i = 0; i<value.size(); ++i){
if (value[i]>max_val){
max_val = value[i];
}
if (value[i]<min_val){
min_val = value[i];
}
sum += value[i];
}
//outputs all values entered (converted to meters)
cout << "\nValues Entered:" << endl;
//sort(value);
for (i = 0; i<value.size(); ++i){
cout << value[i] << " meters" << endl;
}
cout << "Total: " << sum << " meters" << endl;
cout << "Smallest value: " << min_val << endl;
cout << "Largest value: " << max_val << endl;
cout << "Total values entered: " << no_of_inputs << endl;
return 0;
I have an if statement that whenever I implement it, it throws a "read access violation" exception, even though all my other if-statements look pretty similar and work. Everything else works perfectly, so it perplexes me as to why this is happening.
if (records[i].studentArray[j].id == records[i-1].studentArray[j].id) //these two
records[i].retained++;
Full function
void readFile()
{
//Function reads the file and displays all results
int i = 0;
int j = 0;
int count;
Semester records[25]; //only holds up to 25 semesters
Student studentArray;
string semesterName;
ifstream roster;
roster.open ("records.txt");
if (roster.is_open())
{
roster >> semesterName;
while(!roster.eof())
{
roster >> count;
records[i].semesterName = semesterName;
cout << semesterName;
records[i].numStudents = count;
records[i].studentArray = new Student[count];
for (j = 0; j < count; j++)
{
roster >> records[i].studentArray[j];
if (records[i].studentArray[j].year == "FY")
records[i].fycount++;
else if (records[i].studentArray[j].year == "SO")
records[i].socount++;
else if (records[i].studentArray[j].year == "JR")
records[i].jrcount++;
else if (records[i].studentArray[j].year == "SR")
records[i].srcount++;
if (records[i].studentArray[j].id == records[i-1].studentArray[j].id)
records[i].retained++;
}
cout << endl;
cout << "FY: " << records[i].fycount << endl;
cout << "SO: " << records[i].socount << endl;
cout << "JR: " << records[i].jrcount << endl;
cout << "SR: " << records[i].srcount << endl;
cout << "FY gain/loss: " << records[i].fycount - records[i - 1].fycount << endl;
cout << "SO gain/loss: " << records[i].socount - records[i - 1].socount << endl;
cout << "JR gain/loss: " << records[i].jrcount - records[i - 1].jrcount << endl;
cout << "SR gain/loss: " << records[i].srcount - records[i - 1].srcount << endl;
cout << "Percentage gained/lost: " << static_cast<double>(records[i].numStudents - records[i - 1].numStudents) / records[i - 1].numStudents * MULTIPLIER << "%" << endl;
cout << "Number of students retained: " << records[i].retained << endl;
cout << endl;
delete[] records[i].studentArray;
roster >> semesterName;
i++;
}
roster.close();
}
else
cout << "Error: File not found" << endl;
}
Student and semester classes in the .h files
struct Semester
{
string semesterName;
int numStudents;
int fycount = 0;
int socount = 0;
int jrcount = 0;
int srcount = 0;
int retained = 0;
Student *studentArray;
};
class Student
{
public:
string id;
string year;
string name;
string lastName;
friend istream & operator>> (istream &in, Student &s);
};
Thank you!
Do a trace of your application. You will see that i is set to 0 initially and hence -1th location will be invalid in an array.
if (records[i].studentArray[j].id == records[i-1].studentArray[j].id)
That's the source of the error. Better change it to:
if (i > 0 && records[i].studentArray[j].id == records[i-1].studentArray[j].id)
Hope this helps.
I am new to C++ and am trying to build a simple program that with the users input to proceed will generate a random left or right. I had the program working correctly until I added in the array to try and store each item as I have to output them as soon and the user would like to exit the loop. The program seems to compile fine but at run time I receive "Unhandled exception at 0x012B1CA9" Any help would be greatly appreciated.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = '100';
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
for (int i = 1; i < MAX; i++)
{
srand(time(NULL));
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
You have a multi-character constant here... and the behavior doesn't go as expected...
Change this line
const int MAX = '100';
to
const int MAX = 100;
Note the removed single quotes.
And secondly, I will advice you to remove the Seed of the C random generator from the for loop because, you'll likely get the same values from the rand() if you always call it immediately after seeding...
But preferable use the algorithm from C++'s random header
Here is a corrected version of your original code....
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = 100; // <---changed
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
srand(time(NULL)); //< moved to here
for (int i = 0; i < MAX; i++) // <-- modified starting index
{
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
I think that it is basically good idea to read more about C data types and declaration. Your error:
const int MAX = '100' should be const int MAX = 100 without any quotes. C++ does implicit conversion from character literals to int.
This is supposed to read at most 1000 numbers from a file into an array and then analyze them. It works flawlessly unless I use a file with a million numbers in it. I probably because I have an infinite loop, but I can't find where. I'm not supposed to go over 1000 elements.
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
const long N=1000;
using namespace std;
//declaring the functions
int readArray(ifstream& ifile, long arr[]);
void sortArray(long arr[], int numberInTheArray);
int main()
{
//variable declaration
int n=0;
int i=0;
int numberInTheArray=0;
long minimum=0;
long maximum=0;
long arr[N]={0};
ifstream ifile;
string strVar;
cout << "Input File Name: ";
cin >> strVar;
cout << "Which number do you want to return? ";
cin >> i;
cout << endl;
ifile.open(strVar.c_str());
if(!ifile)
{
cout << "That file does not exist!" << endl;
return 1;
}
numberInTheArray = readArray(ifile,arr);
if (numberInTheArray == 0){
cout << "The file is empty!" << endl;
}
else{
maximum = arr[n];
minimum = arr[n];
n++;
while (n<=N){
if (arr[n] <= minimum){
minimum = arr[n];
}
if (arr [n] >= maximum){
maximum = arr[n];
}
n++;
}
cout << "Before Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
sortArray(arr,numberInTheArray);
cout << "\nAfter Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
}
return 0;
}
int readArray(ifstream& ifile, long arr[])
{
int n=0;
long num;
while (n<=N && ifile){
ifile >> arr[n];
n++;
}
n=n-1;
return n;
}
void sortArray(long arr[], int numberInTheArray)
{
int a;
int b;
int temp;
for (a=0; a<numberInTheArray; a++)
{
for (b=0; b<numberInTheArray; b++)
{
if (arr[a] < arr[b])
{
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
}
}
The problem is your loop conditions n <= N. Remember that array indexes goes from zero to size minus one, so the condition should be n < N.