Jump to content

debug program( do not run)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
sureyan

sureyan

    Newbie

  • Members
  • Pip
  • 3 posts
I need your help.
i am trying to output the result in the output file and output to the screen. I could output to the file but I can not output to the screen what can I do ? what do I need to change or add?

thanks

this is the data
storeA 105
storeB 120
storeC 100
storeD 105
storeE 108
storeF 100
storeG 115

and this are the instruction.

A consumer research organization has a data file containing the names of stores and the price that each charges for a particular computer monitor. Write a program to determine the lowest price charged for that monitor and then print two lists.

list one. The names of all stores charging the lowest price.
list two. The names and prices for all stores whose price does not exceed the lowest price by more than 10%

I tried to debug it, but I couldn't do it,

would you please, help me I don't know what it's wrong !!!

thank you









#include <iostream>
#include <string>
#include <cassert>
#include <fstream>

using namespace std;

const int Max_numstores = 10;

int main ()
{
	string names[Max_numstores]; // variables declarations
	double prices [Max_numstores];
	double lowestprice;
	int numstores;

	ifstream filein; // file stream declarations
	ofstream fileoutLp;
	ofstream fileoutLp10;

	filein.open ("stores.txt"); //open a file for imput

	fileoutLp.open ("lp.txt"); // open file for output
	fileoutLp10.open ("lp10.txt");

	lowestprice = 99999.0; //initialization
	numstores = 0;

	// find the lowest price
	// input from input file
	while (filein >> ws && !filein.eof())
	{
	  filein >> names[numstores] >> prices[numstores];

	  if (prices[numstores] < lowestprice)
		lowestprice = prices[numstores];

	  numstores++;
	 }


	 // find stores that not exceed the lowest price by 10%
	 // output from  output files
	 for (int i=0; i<numstores; i++)
	 {
	   if (prices[i] == lowestprice)
		 fileoutLp << names[i] << endl;
	   else if (prices[i] <= lowestprice * 1.1)
		 fileoutLp10 << names[i] << endl;
	 }

	 filein.close();// closing files
	 fileoutLp.close();
	 fileoutLp10.close();

	 return 0;
}







#2
Babbage

Babbage

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi sureyan,

I know that sometimes after looking at your code for so long, sometimes the obvious things start to slip by you. The error with your code (and ultimately, the reason it is currently unable to compile), is because one of your variables names is wrong. On line 24, you say:

fileoutLp7.open ("lp10.txt");

But, a few lines earlier (line 19), you declared it as:

ofstream fileoutLp10;

So basically, if you change line 24 from fileoutLp7.open to fileoutLp10.open, you should be able to get the program to compile.

Good luck!

-Babbage

#3
sureyan

sureyan

    Newbie

  • Members
  • Pip
  • 3 posts
thank you I did change it. It is compiling now and I could output to the output file... but now I can not output to the screen..What do I need to change for that or do?? how can I use std::cout to print to the screen?

thank you....

#4
Babbage

Babbage

    Newbie

  • Members
  • PipPip
  • 11 posts
sureyan, you may want to take a look here for examples of how to use cout:

Examples Using cout

In case you are unfamiliar with it, cout is easy to use. You simply type cout followed by the '<<' operator, then type the variable / string you want to output, followed by another '<<' operator, and finally the newline variable (if you want it), which is endl.

Example:
cout << names[i] << endl;

See, very simple. In fact, it is just like how you output to a file, but instead of outputting to the file, you are outputting to the screen.

You may also want to consider using printf as an alternative.

-Babbage

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Babbage: He's using C++. printf is C, and somewhat more of a hassle than cout because you have to learn all of the format specifiers.

#6
Babbage

Babbage

    Newbie

  • Members
  • PipPip
  • 11 posts

dargueta said:

Babbage: He's using C++. printf is C, and somewhat more of a hassle than cout because you have to learn all of the format specifiers.

Good call. I guess I figured it wouldn't hurt to know both, but I suppose for the purpose of learning C++ it's best to stick with strict STL stuff.

#7
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Yes, but he'll also know what the point of operators are when he switches to C++ :P

#8
sureyan

sureyan

    Newbie

  • Members
  • Pip
  • 3 posts
Thank you, that really helped me with this problem. I sort understand the cout statement, but I got confused when I needed to extract the information from the output file... a lot of errors came out..

thank you again


QUOTE=Babbage;36118]sureyan, you may want to take a look here for
examples of how to use cout:

[url=http://cplus.about.com/od/learning1/ss/clessontwo_4.htm]Examples Using cout[/url]

In case you are unfamiliar with it, cout is easy to use. You simply type cout followed by the '<<' operator, then type the variable / string you want to output, followed by another '<<' operator, and finally the newline variable (if you want it), which is endl.

Example:


See, very simple. In fact, it is just like how you output to a file, but instead of outputting to the file, you are outputting to the screen.

You may also want to consider using [URL="http://www.google.com/search?q=printf+examples&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a"]printf[/URL] as an alternative.

-Babbage[/QUOTE]