Click to See Complete Forum and Search --> : c++ question (set position?)
Fandelem
02-20-2001, 06:06 PM
alright, blah, this is for an assignment, but i just can't find out how to set positions. here is the part of the assignment:
The output is a single line for each farm, with the name of the farm starting in the first position on a line and the bar chart starting in position 30.
okay.. so how do i move the "position" to 30? i thought it was setw() at first, but was sadly mistaken :D
(besides spaces, har har ;o)
thanks,
~kyle
Whipping Boy
02-20-2001, 06:26 PM
You'll need to use ncurses to do this.
man ncurses
and then look at the man pages for each of the functions.
You can also find a really good ncurses tutorial at http://dickey.his.com/ncurses/ncurses-intro.html
f'lar
02-20-2001, 11:01 PM
couple ways, here's a quick one:
cout << farmname;
for (int i=0; i<(30-strlen(farmname)); i++)
cout << ' ';
cout << bar;
setw(30) is another way. You should include iomanip.h, and you have to do someother things (ie setting ios flags), but I don't remember them because I usually just use the first way.
Fandelem
02-20-2001, 11:25 PM
how would setw() work? i need it to *start* at position 30 and then continue to the right. setw would make 30 as the end, and then start print to the left (if you're looking at the screen, direction-wise).
and to follow up on your signature (that's one of the few parts of the bible i've ever read, its fantastic)
.."Love is patient, love is kind, it is not envious. Love does not brag, it is not puffed up. It is not rude, it is not self-serving, it is not easily angered, or resentful. It is not glad about injustice, but rejoices in the truth. It bears all things, believes all things, hopes all things, endures all things. Love never ends."
there's my religious mood for the day ;o)
f'lar
02-21-2001, 02:34 AM
You use the setw for printing the farm name, but you also set a flag so that it still left justifies. Then when it finishes you are at the position 30. Like I said, the quick loop with strlen is easier anyway.
Sterling
02-21-2001, 11:34 AM
Actually, isn't setw cleaner? I don't remember the details of I/O stream manipulation stuff, but won't that clip off anything beyond the 30-character limit in the farm name? I know the for-loop and strlen method won't do that.
Fandelem
02-21-2001, 02:15 PM
but you also set a flag so that it still left justifies.
where can i find info on this? my c++ book doesn't have hardly anything about setting positions.
f'lar
02-23-2001, 01:12 AM
but if the array for the string is only 30 characters anyway...
okay, so c++ doesn't check for that kind of mistake, but for some reason I just never liked doing it that way.
don't look for positions. try checking for flags, or ios::, or iomanip.h header.
[ 23 February 2001: Message edited by: f'lar ]
Fandelem
02-23-2001, 01:23 AM
any suggestions on starting points to look for this stuff? here my code so far:
/************************************************** *****************************
Name Kyle Davis
SSN
Course COP2220.002
Due Date Feb. 20th
Program# 2
Version Microsoft Visual C++ 6.0
{Program Description}
************************************************** ******************************
formulas used:
-------------------------------------------------------------------------------------------------
this will print the name of the farm and space until it reaches position 30
cout << name_of_farm;
for (int i=0; i<(30-strlen(name_of_farm)); i++)
{
cout << ' ';
}
-------------------------------------------------------------------------------------------------
this will loop until acre_planted hits zero or symbolcounter hits 23 (for overflow purposes)
and print a * for every acre (which represents 250 pints) and temppints will eventually
hit 5000 because it's being incremented by 250 each time.
while ((0 < acre_planted) && symbolcounter < 23)
{
cout << '*';
if (temppints == 5000)
{
cout << '#';
}
temppints = temppints + 250;
acre_planted = acre_planted - 1;
symbolcounter++;
if (symbolcounter == 22)
{
cout << "(overflow)" << endl;
// this will exit the loop but it's a poor way of doing it -
// unsure how to "exit" out of while's otherwise.
symbolcounter++;
}
************************************************** ******************************/
#include <iostream> // for cin & cout
#include <iomanip> // for setw()
using namespace std;
/* declare prototypes */
void PrintHeading();
void ProgramPurpose();
int main()
{
char name_of_farm[29];
float acre_planted = 0;
int pints_produced = 0;
/* let's print the program purpose */
ProgramPurpose();
cout << "Please enter the name of the farm and press return." << endl;
cin.get(name_of_farm, 29);
cin.ignore(100, '\n');
do
{
cout << "Alright, for " << name_of_farm << ", please enter the "
<< "acres planted and press return." << endl;
cin >> acre_planted;
if (!cin)
{
cerr << "\nInvalid Acreage.\n" << endl;
cout << "If you wish to quit, press ctrl+c, otherwise:\n" << endl;
cin.clear(); // clear failure flag
cin.sync(); // flush buffer
}
} while ( acre_planted <= 0 );
do
{
cout << "Please enter how many pint jars of popcorn were produced" << endl;
cin >> pints_produced;
if (!cin)
{
cerr << "\nInvalid Amount of Pints.\n" << endl;
cout << "If you wish to quit, press ctrl+c, otherwise:\n" << endl;
cin.clear(); // clear failure flag
cin.sync(); // flush buffer
}
} while ( pints_produced <= 0 );
/* print heading information */
PrintHeading();
/* this will print the name of the farm and space until it reaches position 30 */
cout << name_of_farm;
for (int i=0; i<(30-strlen(name_of_farm)); i++)
{
cout << ' ';
}
int symbolcounter = 0; // symbolcounter is to make sure it doesn't overflow
int temppints = 500; // temppints is to check for when it reaches 5000
/*
this will loop until acre_planted hits zero or symbolcounter hits 23 (for overflow purposes)
and print a * for every acre (which represents 250 pints) and temppints will eventually
hit 5000 because it's being incremented by 250 each time.
*/
while ((0 < acre_planted) && symbolcounter < 23)
{
cout << '*';
if (temppints == 5000)
{
cout << '#';
}
temppints = temppints + 250;
acre_planted = acre_planted - 1;
symbolcounter++;
if (symbolcounter == 22)
{
cout << "(overflow)" << endl;
// this will exit the loop but it's a poor way of doing it -
// unsure how to "exit" out of while's otherwise.
symbolcounter++;
}
}
cout << endl;
return 0;
}
void ProgramPurpose()
{
cout << "This program will produce a bar chart of" << endl
<< "gourmet-popcorn production for a cooperative" << endl
<< "farm group on a farm-by-farm basis." << endl << endl << endl;
}
void PrintHeading()
{
system("cls");
cout << "Farm Name" << setw(34) << "Production in" << endl
<< setw(42) << "Thousands of" << endl
<< setw(48) << "Pint Jars per Acre" << endl;
cout << setw(53) << " 1 2 3 4 5 6" << endl;
cout << setw(53) << "---|---|---|---|---|---" << endl;
return;
}
and no, it doesn't work properly ;o) the project is suppose to:
Develop a functional decomposition and write a program to produce a bar chart of gourmet-popcorn production for a cooperative farm group on a farm-by-farm basis. The input to the program is a series of data sets, one per line, with each set representing the productino of one farm. The output is a bar chart that identifies each farm and displays its productino in pints of corn per acre. The output is a single line for each farm, with the name of the farm starting in the first position on the line and the bar chart starting in position 30. Each mark in the bar chart represents 250 jars of popcorn per acre. The productino goal for the year is 5000 jars per acre. A vertical bar should appear in the chart for farms with lower production, and a special mark is used for farms with productino greater than or equal to 5000 jars per acre (denoted by the #)
(excuse the typos; typing blindly ;o)
anyways, my algorithm doesn't work. blah. but does everything else look alright?
thanks for all your help man.
~kyle
f'lar
02-23-2001, 02:24 PM
define "lower production".