This program is supposed to simply take user input and output it in the right format. It keeps giving me an error message. I cant figure out what is wrong and would appreciate some help.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    string date[9], dollars[3], cents[3], first_name[20], last_name[20], payee[50];

    cout << "Please input the following data in order the you are prompted, pressing enter after each input: " <<endl;
    cout << "Date (m/dd/yyyy): ";
    cin >> date[9];
    cout << "First name: ";
    cin >> first_name[20];
    cout << "Last name: ";
    cin >> last_name[20];
    cout << "Using numbers, please enter the two digist amount of the check in whole dollars: ";
    cin >> dollars[2];
    cout << "Using number only, please enter the two digit value of the remaining change that you are owed: ";
    cin >> cents[2];
    cout << "Who is the payee in this transaction? <<endl";
    cin >> payee[50];

    cout << first_name[20] << " " << last_name[20] << setw(10) <<endl;
    cout << "pay to: " << payee[50] << setw(10) << dollars[2] << "." << cents[2];
}

Please be specific. Simply saying "It keeps giving me an error message" is an almost completely useless statement. What is the specific error?

For starters, you clearly don't know how arrays don't work. The declaration:

std::string date[9]

declares an array of 9 strings numbered 0-8, not a 9-character-long char array. Thus, when you use the input statement

cin >> date[9]

you are attempting to input to element 9 of your array which does not exist. This type of error would typically cause a "segmentation fault". You do it for every one of your variables and inputs.

You are right. Sorry. I am running on very little sleep. I am no longer getting that error message. I had tried a character array before and couldn't get it to work. What I had made no sense. My real question though actually about setw. My problem is that I want to take the user's input and line it up. I've tried to google the syntax for setw, but neither my book or the internet sources I looked at have led me to correct formatting. There is more to this program, but i'm just trying to get an idea for setw.

Here is my new code:

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

int main()
{
    char first_name[9], last_name[20], payee[50], date[9], dollars[3], cents[3];

    cout << "Please input the following data in order the you are prompted, pressing enter after each input: " <<endl;
    cout << "Date (mm/dd/yyyy): ";
    cin >> date;
    cout << "First name: ";
    cin >> first_name;
    cout << "Last name: ";
    cin >> last_name;
    cout << "Using numbers, please enter the two digist amount of the check in whole dollars: ";
    cin >> dollars;
    cout << "Using number only, please enter the two digit value of the remaining change that you are owed: ";
    cin >> cents;
    cout << "Who is the payee in this transaction?" <<endl;
    cin >> payee;

    cout << first_name << " " << last_name << setw(10) <<endl;
    cout << "pay to: " << payee << setw(10) << dollars << "." << cents;
}

Input of...
date: 03/21/2011
first name: John
Last name: Connor
Dollar amount: 21
Change: 14
payee: first financial

should output to:
03/21/2011 John Connor
21.14 first financial

I've tried to google the syntax for setw, but neither my book or the internet sources I looked at have led me to correct formatting. There is more to this program, but i'm just trying to get an idea for setw.

This search gives the correct formatting for setw . Don't know what you were searching for...

I know this must be really simple for you guys. It may be simple, but its not like I haven't searched google. I've sat here for the past hour trying to figure it out without asking dumb questions. What i'm trying to do is set the argument for within setw to a number which changes based on what came before it. It keeps telling me strlen() is not declared and I should first use this function. It is not recognizing what strlen() is, although I don't know why.

I'm just asking what I am doing wrong in implemeting setw. From the explanations I've found I'm doing it right, but even when I put in set numbers like setw(10)in the last few lines of the code below it doesnt put any spaces between the characters for first_name/last_name and the date.

#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    char first_name[9], last_name[20], date[9], dollars[3], cents[3];
    string payee;

    cout << "Please input the following data in order the you are prompted, pressing enter after each input: " <<endl;
    cout << "Date (mm/dd/yyyy): ";
    cin >> date;
    cout << "First name: ";
    cin >> first_name;
    cout << "Last name: ";
    cin >> last_name;
    cout << "Using numbers, please enter the two digist amount of the check in whole dollars: ";
    cin >> dollars;
    cout << "Using number only, please enter the two digit value of the remaining change that you are owed: ";
    cin >> cents;
    cout << "Who is the payee in this transaction?" <<endl;
    cin >> payee;
    cout <<endl;

    cout << first_name << " " << last_name << setw(40-(1 + strlen(first_name) + strlen(last_name))) << date <<endl;
    cout << "pay to: " << payee << setw(40- (str.length(payee) + 8)) << dollars << "." << cents;
}

setw(x) -- Sets the number of characters (x) to be used as the field width for the next insertion operation.

It does not output x spaces. How many characters in total do you want the payee to output? Put that number in x. If you choose 20 and payee has 10, you will get 10 spaces + the payee.

If you want the columns consistent for each output, don't try to calculate the width, you're likely to get varied results. You should use some sort of constant for the argument to setw():

//...

const int DATE_FIELD_WIDTH = 12, SOME_OTHER_WIDTH = 5;

//...

int main() {

  //...

  cout << setw(DATE_FIELD_WIDTH) << dateString << " " << setw(SOME_OTHER_WIDTH) << someOtherValue << endl;

  //...
}

date: 03/21/2011
first name: John
Last name: Connor
Dollar amount: 21
Change: 14
payee: first financial

should output to:

03/21/2011          John Connor
21.14               first financial

With the current setup, you can use the length of the date as your field width because it will always be longer than the amount. However, in a more robust system, the amount could potentially be longer or shorter than the date, in which case it makes more sense to check both and use the longer of the two to set your field width:

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

using namespace std;

int main()
{
    string date, first_name, last_name, dollars, cents, payee;

    cout<<"Date: ";
    getline(cin, date);
    cout<<"First name: ";
    getline(cin, first_name);
    cout<<"Last name: ";
    getline(cin, last_name);
    cout<<"Dollar amount: ";
    getline(cin, dollars);
    cout<<"Change: ";
    getline(cin, cents);
    cout<<"Payee: ";
    getline(cin, payee);
    cout<<'\n';

    int field_width = date.size();
    string amount = dollars + '.' + cents;

    if (amount.size() > field_width)
        field_width = amount.size();

    field_width += 10; // To match your specified output

    cout<< setw(field_width) << left << date << first_name <<' '<< last_name <<'\n'
        << setw(field_width) << left << amount << payee <<'\n';
}

setw(x) -- Sets the number of characters (x) to be used as the field width for the next insertion operation.

It does not output x spaces. How many characters in total do you want the payee to output? Put that number in x. If you choose 20 and payee has 10, you will get 10 spaces + the payee.

ok, now that what I thought that it would do. I looked at several examples online, but when I use the syntax:

cout << first_name << " " << last_name << setw(15) << date <<endl;
    cout << "pay to: " << payee << setw(15) << dollars << "." << cents;

I still got the date pressed right up against the name, like the first setw() wasn't working. That makes perfect sense though since I have just the dollar amount set as the next insertion operator.

I get it now. Can you tell i'm a biology student, not computer science? Ask me to do organic chemistry or cell bio i'm fine. Put me in front of a computer and I feel like my grandpa whining about how the series of tubes is too hard to use...

With the current setup, you can use the length of the date as your field width because it will always be longer than the amount. However, in a more robust system, the amount could potentially be longer or shorter than the date, in which case it makes more sense to check both and use the longer of the two to set your field width:

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

using namespace std;

int main()
{
    string date, first_name, last_name, dollars, cents, payee;

    cout<<"Date: ";
    getline(cin, date);
    cout<<"First name: ";
    getline(cin, first_name);
    cout<<"Last name: ";
    getline(cin, last_name);
    cout<<"Dollar amount: ";
    getline(cin, dollars);
    cout<<"Change: ";
    getline(cin, cents);
    cout<<"Payee: ";
    getline(cin, payee);
    cout<<'\n';

    int field_width = date.size();
    string amount = dollars + '.' + cents;

    if (amount.size() > field_width)
        field_width = amount.size();

    field_width += 10; // To match your specified output

    cout<< setw(field_width) << left << date << first_name <<' '<< last_name <<'\n'
        << setw(field_width) << left << amount << payee <<'\n';
}

this makes sense, and I would have gone that route, but the getline function on the compiler we're using for this class is broken and thus we werent allowed to use it. Thank you for the suggestions, though. I appreciate the time you've taken to help me with my problem.

but the getline function on the compiler we're using for this class is broken

Visual C++ 6? If so, there's a knowledgebase article on how to fix it. You really have no choice but to use getline or some facsimile because in your example the payee string has multiple words separated by whitespace. The >> operator stops reading at whitespace, so "first financial" would be stored as "first" and leave "financial" in the stream.

Visual C++ 6? If so, there's a knowledgebase article on how to fix it. You really have no choice but to use getline or some facsimile because in your example the payee string has multiple words separated by whitespace. The >> operator stops reading at whitespace, so "first financial" would be stored as "first" and leave "financial" in the stream.

Right. I will have to have the user input two separate arrays of characters. I'm using eclipse with cygwin. I believe getline is broken in cygwin. It's not good but its what we can do, though dumb.

I have to ask my professor how he wants to fudge this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.