Question

C++: Need help debugging my code

I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages.

//main.cpp
//Semester Project
//Created by J---on 5/6/2019

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <bits/stdc++.h>

using namespace std;

void instructions(); //displays program details and instructions
void openFile();
void takeInput(int*);
void switchBoard(int*);

struct price
{
int day, month, year;
float cost;
};

bool compareLow(price a, price b)
{
return a.cost < b.cost;
}
bool compareHigh(price a, price b)
{
return a.cost > b.cost;
}

int main()
{
int input;
instructions(); //display instructions & menu
takeInput(&input); //selection from menu on what to process
switchBoard(&input);//uses a switch-case to call the respective function
  
  
  
  
  
return 0;
}


void instructions()
{
cout << "|Semester Project\n";
cout << "|This program takes the .txt file 'GasPrices.txt' and\n";
cout << "|sorts lists the following based on user input:\n";
cout << "|\t1) Average Price Per Year\n";
cout << "|\t2) Average Price Per Month\n";
cout << "|\t3) Data from high to low\n";
cout << "|\t4) Data from low to high\n";
cout << endl;
}

void takeInput(int* input)
{
  
do //simple error catching loop
{
if(*input > 5)
{
cout << "|ERROR -> Improper input\n";
}
cout << "|Enter the number for the respective process or 5 to exit\n";
cout << "|Menu Number:\t";
cin >> *input;
cout << endl;
  
}while(*input > 5);
}

void switchBoard(int* input)
{
price data[1000];
int i=0, j, year, cnt, month;
float avg, min, max;
string temp, temp_day, temp_month, temp_year, temp_price;
cout << "|Opening File...\n";
ifstream file("GasPrices.txt");
if(!file.is_open())
{
cout << "\t|ERROR -> File not found\n";
cout << "\t|Closing Program...\n";
exit(EXIT_FAILURE);
}
while(!file.eof()) // reading from the file
{
file>>temp;
temp_month=temp.substr(0,2);
temp_day=temp.substr(3,2);
temp_year=temp.substr(6,4);
temp_price=temp.substr(11,temp.size()-10);

stringstream geek1(temp_day);
stringstream geek2(temp_month);
stringstream geek3(temp_year);
stringstream geek4(temp_price);

geek1>>data[i].day;
geek2>>data[i].month;
geek3>>data[i].year;
geek4>>data[i].cost;

i++;
}
  
  
switch(*input)
{
case 1:
for(year=1993;year<1995;year++)
{
cnt=0;
avg=0;
max=data[0].cost;
min=data[0].cost;
for(j=0;j<i;j++)
{
if(data[j].year==year)
{
avg+=data[j].cost;
cnt++;
}
if(data[j].cost<min)
min=data[j].cost;

if(data[j].cost>max)
max=data[j].cost;
}
cout<<"Year: "<<year<<" Average Price: "<<avg/cnt<<endl;

cout<<"Highest price: "<<max<<" Lowest price: "<<min<<endl;
cout<<endl;
}

cout<<endl;
break;
  
case 2:
for(year=1993;year<1995;year++)
{
for(month=1;month<13;month++)
{
cnt=0;
avg=0;
for(j=0;j<i;j++)
{
if(data[j].year==year && data[j].month==month)
{
avg+=data[j].cost;
cnt++;
}
}
cout<<"Year: "<<year<<" Month : "<<month<<" Average Price: "<<avg/cnt<<endl;
}
}
break;
case 3:
sort(data,data+i,compareLow);
ofstream file2("High-Low.txt");
for(j=0;j<i;j++)
{
file2<<data[j].month<<"-"<<data[j].day<<"-"<<data[j].year<<":"<<data[j].cost<<endl;
}
file2.close();
break;
case 4:
sort(data,data+i,compareHigh);
ofstream file1("Low-High.txt");
for(j=0;j<i;j++)
{
file1<<data[j].month<<"-"<<data[j].day<<"-"<<data[j].year<<":"<<data[j].cost<<endl;
}
file1.close();
break;
case 5:
cout << "|Closing Program...";
exit(EXIT_FAILURE);
}
}


This is my error message:

main.cpp: In function void switchBoard (int*) main.cpp:174:14: error: jump to case label [-fpermissive] case 4: main cpp:167

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ofstream file2("High-Low.txt");

ofstream file1("Low-High.txt");

Declare the above two statements outside switch - case, in side the function beginning.

If you will declare any object inside the case it will show the error message jump to case label.

Because declaring an object will call the constructor of the class which will jump from the case constant.

Add a comment
Know the answer?
Add Answer to:
C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide brief comments in the top code to show what is happening? THE CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;    }       for...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • im writing a c++ code and getting stuck on two parts. The first part, when I...

    im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • Can someone please help me. i keep getting an error in my code. I need it...

    Can someone please help me. i keep getting an error in my code. I need it to be on three seperate files! attached is my assignment and my code. c++ The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT