Question

Hey, so i am trying to have my program read a text file using a structure...

Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted the txt file into the same folder as this program so i thought it would work, any help is greatly appreciated.

(C++ Visual Studio 2017)

#include <iostream>  
#include <iomanip>  
#include <string>  
#include <fstream>


using namespace std;

string strFilename = "";
struct nah
{
   int intProductid = 0;
   int intQuantity = 0;
   float flSellprice = 0;
   float flProductcost = 0;
   float flProfit = 0;
   int intProductid2 = 0;
   int intQuantity2 = 0;
   float flSellprice2 = 0;
   float flProductcost2 = 0;
   char chChoice = ' ';
}numbers;

int main()
{
   cout << "***********************************************" << endl;
   cout << "** Enter the file name you want to read from **" << endl;
   cout << "***********************************************" << endl;
   cin >> strFilename;
   system("cls");

   ifstream in(strFilename.c_str());
   if (!in.fail())
   {
       cout << "File Read Error, try again after a bit" << endl;
       system("cls");
   }
   else
   {
       in >> numbers.intProductid;
       in >> numbers.flProductcost;
       in >> numbers.intQuantity;
       in >> numbers.flSellprice;
       cout << "*********************************" << endl;
       cout << "Product ID: " << numbers.intProductid << endl;
       cout << "Product Cost: " << numbers.flProductcost << endl;
       cout << "Quantity: " << numbers.intQuantity << endl;
       cout << "Sell Price: " << numbers.flSellprice << endl;
       cout << "*********************************" << endl;
       system("pause");
       system("cls");
   }

   do
   {
       cout << "\t\t--Menu--" << endl;
       cout << "1) Edit Output" << endl;
       cout << "2) New Output" << endl;
       cout << "e) Exit" << endl;
       cin >> numbers.chChoice;
       system("cls");

       switch (numbers.chChoice)
       {
       case '1':
           cout << "--Here is the current output--" << endl;
           cout << "*********************************" << endl;
           cout << "Product ID: " << numbers.intProductid << endl;
           cout << "Product Cost: " << numbers.flProductcost << endl;
           cout << "Quantity: " << numbers.intQuantity << endl;
           cout << "Sell Price: " << numbers.flSellprice << endl;
           cout << "*********************************" << endl;
           cout << "Please enter the changes you would like to make "
               << "(if no changes are to be made"
               << " please enter the same value)" << endl;
           cout << "Productid: ";
           cin >> numbers.intProductid2;
           cout << "Product Cost: ";
           cin >> numbers.flProductcost2;
           cout << "Quantity: ";
           cin >> numbers.intQuantity2;
           cout << "Sell Price: ";
           cin >> numbers.flSellprice2;
           system("cls");

       case '2':
           cout << "--Here is your new output after edits--" << endl;
           cout << "Product ID: " << numbers.intProductid2 << endl;
           cout << "Product Cost: " << numbers.flProductcost2 << endl;
           cout << "Quantity: " << numbers.intQuantity2 << endl;
           cout << "Sell Price: " << numbers.flSellprice2 << endl;

       case 'E':

       case 'e': //Exit
           break;

       default:
           cout << "*****************************" << endl;
           cout << "** pick a different option **" << endl;
           cout << "*****************************" << endl;
           system("pause");
           system("cls");
           break;
       }
   } while (numbers.chChoice != 'E' && numbers.chChoice != 'e');

   return 0;
}

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

Below is the corrected code. The problem was it was having !in.fail() in the if statement

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include<cstdlib>

using namespace std;

string strFilename = "";
struct nah
{
int intProductid = 0;
int intQuantity = 0;
float flSellprice = 0;
float flProductcost = 0;
float flProfit = 0;
int intProductid2 = 0;
int intQuantity2 = 0;
float flSellprice2 = 0;
float flProductcost2 = 0;
char chChoice = ' ';
}numbers;

int main()
{
cout << "***********************************************" << endl;
cout << "** Enter the file name you want to read from **" << endl;
cout << "***********************************************" << endl;
cin >> strFilename;
system("cls");

ifstream in(strFilename.c_str());
if (in.fail())
{
cout << "File Read Error, try again after a bit" << endl;
system("cls");
}
else
{
in >> numbers.intProductid;
in >> numbers.flProductcost;
in >> numbers.intQuantity;
in >> numbers.flSellprice;
cout << "*********************************" << endl;
cout << "Product ID: " << numbers.intProductid << endl;
cout << "Product Cost: " << numbers.flProductcost << endl;
cout << "Quantity: " << numbers.intQuantity << endl;
cout << "Sell Price: " << numbers.flSellprice << endl;
cout << "*********************************" << endl;
system("pause");
system("cls");
}

do
{
cout << "\t\t--Menu--" << endl;
cout << "1) Edit Output" << endl;
cout << "2) New Output" << endl;
cout << "e) Exit" << endl;
cin >> numbers.chChoice;
system("cls");

switch (numbers.chChoice)
{
case '1':
cout << "--Here is the current output--" << endl;
cout << "*********************************" << endl;
cout << "Product ID: " << numbers.intProductid << endl;
cout << "Product Cost: " << numbers.flProductcost << endl;
cout << "Quantity: " << numbers.intQuantity << endl;
cout << "Sell Price: " << numbers.flSellprice << endl;
cout << "*********************************" << endl;
cout << "Please enter the changes you would like to make "
<< "(if no changes are to be made"
<< " please enter the same value)" << endl;
cout << "Productid: ";
cin >> numbers.intProductid2;
cout << "Product Cost: ";
cin >> numbers.flProductcost2;
cout << "Quantity: ";
cin >> numbers.intQuantity2;
cout << "Sell Price: ";
cin >> numbers.flSellprice2;
system("cls");

case '2':
cout << "--Here is your new output after edits--" << endl;
cout << "Product ID: " << numbers.intProductid2 << endl;
cout << "Product Cost: " << numbers.flProductcost2 << endl;
cout << "Quantity: " << numbers.intQuantity2 << endl;
cout << "Sell Price: " << numbers.flSellprice2 << endl;

case 'E':

case 'e': //Exit
break;

default:
cout << "*****************************" << endl;
cout << "** pick a different option **" << endl;
cout << "*****************************" << endl;
system("pause");
system("cls");
break;
}
} while (numbers.chChoice != 'E' && numbers.chChoice != 'e');

return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Hey, so i am trying to have my program read a text file using a structure...
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
  • Hello i am having a bit of trouble with a verified exit for my program. For...

    Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() {    char chChoice3 = ' ';    do    {        cout << "\n\t--Menu--" << endl;        cout << "1) Enter Player Name" <<...

  • Hey, i was just wondering how i would calculate over time pay in c++ visual studio...

    Hey, i was just wondering how i would calculate over time pay in c++ visual studio 2017? what i have right now is either returning 0 for some reason or being skipped over? im also wondering about the federal tax as well because that also doesn't seem to work, any help is greatly appreciated! these are my variables char chChoice = ' '; int intempID = 0; int intHours = 0; int intOThours = 0; float flOTrate = 0; float...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • I need help with my coding for C++! The goal of this is to calculate the...

    I need help with my coding for C++! The goal of this is to calculate the total cost of a product by combining the products price and the amount of tax, then display it to the user. The numbers we have to test out is 100 for the price and 8.25 for the percentage. When I test is out, the formula works but it isn't rounding the 108.25 to 108.3, which is the total price we are supposed to display...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer {    int accountNumber;    string customerFullName;    string customerEmail;    double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) {    cout << fixed << setprecision(2);    for (int i = 0; i...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

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