In case the guys who helped me with a "pass-by-reference" function didn't see it, thank you for your help!

I've only got one problem right now and thats that my program will not send data to an output file. Here's the code:

bool checkInputFile(ifstream &fin);  // checks the file input
void changezipcode(int zipcode, string &newzipcode); // this is the function that processes the data from numbers to words

int main()
{
    ifstream fin;
    ofstream fout;

    string firstName;
    string lastName;
    int houseNumber, city, street, state;
    int zipcode1;
    int zipcode;
    string newzipcode;

    fin.open("program5.txt");
    fout.open("output5.txt");

    if (! checkInputFile(fin))
    {
        exit(2);
    }

    if(fout.fail())
    {
        cerr << "Unable to open output file\n";
        exit(3);
    }


    while(fin >> firstName >> lastName >> houseNumber >> street >> city >> state >> zipcode1)
    {
        zipcode1 = zipcode;
    changezipcode(zipcode, newzipcode); // Function Call
    fout << setw(10) << firstName << setw(10) << lastName << setw(10) << street << setw(10) << city << setw(10) << state << setw(10) << zipcode << endl;
    }
    
    cout << "Please view the Outputfile: output5.txt\n"; // Prompts user to see the output

    fin.close();
    fout.close();
    return 0;
}

bool checkInputFile(ifstream &fin) // Checks input
{
    if(fin.good())
    {
        return true;
    }
    else
    {
        cerr << "Unable to open input file\n";
        return false;
    }
}
void changezipcode(int zipcode, string& newzipcode) // The function
{
    int div=10000;
    string parse="";
    int temp;

    while(div>0)
    {
        temp=(zipcode/div);
        switch (temp)
        {
        case '0': parse+="zero"; break;
        case '1': parse+="one"; break;
        case '2': parse+="two"; break;
        case '3': parse+="three"; break;
        case '4': parse+="four"; break;
        case '5': parse+="five"; break;
        case '6': parse+="six"; break;
        case '7': parse+="seven"; break;
        case '8': parse+="eight"; break;
        case '9': parse+="nine"; break;
        }
        zipcode=zipcode-(temp*div);
        div=div/10;
    }
    newzipcode=parse;
    return;
}

It all LOOKS good. Upon comparing it to several other program though, I can't figure out why its not taking the information and placing it into an output file. Can anyone tell me why? Also, is there anything wrong with my function call?

Recommended Answers

All 7 Replies

Could u post our input file too amigo? :D

Sidatra79, here it is:

JAMES SMITH 792 Cherry Fairway MO 64869
MARY JOHNSON 209 Maple Leawood MO 64418
JOHN WILLIAMS 561 Elder Raytown MO 64512
PATRICIA JONES 298 Oak Riverside KS 64655
ROBERT BROWN 705 Birch Fairway MO 64712
LINDA DAVIS 307 Peach Riverside KS 64415
MICHAEL MILLER 973 Birch Olathe KS 64457
BARBARA WILSON 1029 Apple Riverside MO 64028
WILLIAM MOORE 670 Apple Parkville KS 64124
ELIZABETH TAYLOR 439 Maple Leawood MO 64558
DAVID ANDERSON 189 Apple Olathe KS 64305
JENNIFER THOMAS 550 Birch Leawood KS 64371
RICHARD JACKSON 372 Cherry Fairway MO 64452
MARIA WHITE 810 Elder Riverside KS 64165
CHARLES HARRIS 993 Cherry Leawood KS 64305
SUSAN MARTIN 1023 Cherry Raytown KS 64511
JOSEPH THOMPSON 647 Oak Parkville MO 64680
MARGARET GARCIA 265 Birch Fairway MO 64989
THOMAS MARTINEZ 964 Maple Riverside KS 64723
DOROTHY ROBINSON 462 Oak Olathe MO 64301
CHRISTOPHER CLARK 313 Oak Olathe MO 64739
LISA RODRIGUEZ 909 Cherry Parkville KS 64290
DANIEL LEWIS 882 Maple Parkville KS 64481
NANCY LEE 723 Peach Fairway KS 64258
PAUL WALKER 637 Walnut Leawood MO 64538
KAREN HALL 849 Walnut Fairway MO 64312
MARK ALLEN 996 Peach Raytown MO 64399
BETTY YOUNG 391 Birch Raytown MO 64307
DONALD HERNANDEZ 356 Peach Raytown MO 64424
HELEN KING 289 Peach Raytown KS 64287
GEORGE WRIGHT 595 Maple Riverside KS 64627
SANDRA LOPEZ 403 Maple Leawood KS 64892
KENNETH HILL 287 Birch Leawood MO 64463
DONNA SCOTT 805 Apple Fairway KS 64962
STEVEN GREEN 348 Walnut Raytown MO 64995
CAROL ADAMS 78 Walnut Olathe KS 64861
EDWARD BAKER 878 Birch Raytown MO 64225
RUTH GONZALEZ 1014 Peach Riverside MO 64405
BRIAN NELSON 40 Peach Leawood MO 64415
SHARON CARTER 179 Apple Fairway KS 64297
RONALD MITCHELL 583 Birch Riverside KS 64319
MICHELLE PEREZ 80 Cherry Fairway MO 64372
ANTHONY ROBERTS 408 Birch Riverside MO 64057
LAURA TURNER 713 Apple Fairway MO 64176
KEVIN PHILLIPS 659 Elder Fairway MO 64225
SARAH CAMPBELL 983 Apple Fairway MO 64315
JASON PARKER 750 Walnut Olathe MO 64033
KIMBERLY EVANS 132 Apple Leawood KS 64256
MATTHEW EDWARDS 173 Maple Parkville KS 64031
DEBORAH COLLINS 214 Peach Leawood KS 64387
GARY STEWART 270 Walnut Olathe MO 64813
JESSICA SANCHEZ 556 Walnut Olathe KS 64591
TIMOTHY MORRIS 524 Walnut Leawood MO 64007
SHIRLEY ROGERS 718 Oak Riverside MO 64041
JOSE REED 974 Oak Riverside KS 64600
CYNTHIA COOK 739 Oak Leawood KS 64115
LARRY MORGAN 715 Oak Raytown KS 64111
ANGELA BELL 1027 Maple Leawood MO 64469
JEFFREY MURPHY 531 Walnut Riverside MO 64762
MELISSA BAILEY 197 Oak Raytown MO 64045
FRANK RIVERA 695 Walnut Olathe MO 64948
BRENDA COOPER 845 Apple Olathe MO 64361
SCOTT RICHARDSON 246 Oak Fairway MO 64849
AMY COX 187 Oak Fairway KS 64082
ERIC HOWARD 536 Oak Fairway MO 64204
ANNA WARD 925 Elder Leawood MO 64544
STEPHEN TORRES 703 Apple Fairway MO 64590
REBECCA PETERSON 296 Apple Leawood MO 64195
ANDREW GRAY 922 Cherry Leawood MO 64553
VIRGINIA RAMIREZ 541 Elder Riverside MO 64777
RAYMOND JAMES 664 Maple Fairway KS 64426
KATHLEEN WATSON 383 Walnut Riverside MO 64616
GREGORY BROOKS 942 Maple Leawood MO 64807
PAMELA KELLY 149 Maple Parkville MO 64771
JOSHUA SANDERS 821 Apple Raytown KS 64017
MARTHA PRICE 643 Elder Parkville KS 64088
JERRY BENNETT 196 Cherry Parkville KS 64327
DEBRA WOOD 907 Apple Olathe KS 64419
DENNIS BARNES 649 Oak Leawood KS 64071
AMANDA ROSS 305 Walnut Olathe KS 64148
WALTER HENDERSON 156 Peach Riverside MO 64380
STEPHANIE COLEMAN 210 Apple Parkville KS 64435
PATRICK JENKINS 820 Birch Parkville MO 64610
CAROLYN PERRY 715 Peach Parkville MO 64388
PETER POWELL 836 Elder Fairway KS 64909
CHRISTINE LONG 366 Walnut Olathe MO 64740
HAROLD PATTERSON 724 Walnut Parkville MO 77777
MARIE HUGHES 974 Maple Parkville MO 64201
DOUGLAS FLORES 196 Elder Olathe KS 64652
JANET WASHINGTON 326 Walnut Parkville KS 64983
HENRY BUTLER 944 Birch Olathe MO 64586
CATHERINE SIMMONS 534 Cherry Fairway MO 64853
CARL FOSTER 160 Cherry Raytown KS 64606
FRANCES GONZALES 539 Elder Olathe MO 64080
ARTHUR BRYANT 84 Maple Olathe MO 64080
ANN ALEXANDER 383 Walnut Raytown MO 64463
RYAN RUSSELL 237 Oak Parkville KS 64155
JOYCE GRIFFIN 815 Elder Raytown MO 64876
ROGER DIAZ 169 Cherry Olathe KS 64815
DIANE HAYES 879 Cherry Raytown MO 64683
JOE MYERS 767 Birch Raytown KS 64944
ALICE FORD 993 Walnut Parkville MO 64012
JUAN HAMILTON 413 Birch Raytown KS 64368
JULIE GRAHAM 970 Cherry Fairway MO 64168
JACK SULLIVAN 610 Walnut Riverside KS 64027
HEATHER WALLACE 192 Walnut Riverside MO 64978
ALBERT WOODS 875 Elder Raytown KS 64978
TERESA COLE 501 Peach Leawood MO 64187
JONATHAN WEST 863 Oak Raytown KS 64631
DORIS JORDAN 845 Peach Olathe MO 64403
JUSTIN OWENS 876 Apple Riverside KS 64050
GLORIA REYNOLDS 503 Peach Fairway MO 64828
TERRY FISHER 425 Peach Olathe MO 64431
EVELYN ELLIS 285 Oak Parkville MO 64669
GERALD HARRISON 925 Elder Parkville MO 64905
JEAN GIBSON 364 Oak Parkville KS 64327
KEITH MCDONALD 969 Cherry Riverside KS 64063
CHERYL CRUZ 238 Elder Olathe KS 64418
SAMUEL MARSHALL 586 Elder Raytown KS 64679
MILDRED ORTIZ 211 Birch Riverside KS 64501
WILLIE GOMEZ 229 Cherry Olathe KS 64625
KATHERINE MURRAY 226 Walnut Riverside MO 64125
RALPH FREEMAN 763 Walnut Raytown KS 64854
JOAN WELLS 514 Oak Fairway MO 64036
LAWRENCE WEBB 271 Walnut Olathe MO 64717
ASHLEY SIMPSON 762 Apple Raytown KS 64772
NICHOLAS STEVENS 333 Peach Parkville MO 64266
JUDITH TUCKER 328 Peach Olathe KS 64860
ROY PORTER 285 Walnut Olathe KS 64733
ROSE HUNTER 61 Apple Parkville MO 64622
BENJAMIN HICKS 524 Elder Olathe MO 64967
JANICE CRAWFORD 51 Walnut Leawood KS 64794
BRUCE HENRY 925 Birch Olathe KS 64383
KELLY BOYD 264 Cherry Riverside KS 64927
BRANDON MASON 167 Birch Leawood KS 64514
NICOLE MORALES 251 Elder Olathe KS 64506
ADAM KENNEDY 723 Birch Riverside MO 64003
JUDY WARREN 391 Cherry Raytown MO 64313
HARRY DIXON 716 Maple Parkville MO 64812
CHRISTINA RAMOS 350 Oak Parkville KS 64536
FRED REYES 589 Oak Riverside KS 64207
KATHY BURNS 620 Maple Riverside MO 64764
WAYNE GORDON 157 Oak Leawood MO 64763
THERESA SHAW 437 Maple Raytown MO 64231
BILLY HOLMES 246 Apple Olathe KS 64313
BEVERLY RICE 111 Peach Parkville MO 64185
STEVE ROBERTSON 754 Oak Raytown MO 64036
DENISE HUNT 758 Peach Parkville KS 64950
LOUIS BLACK 761 Walnut Raytown MO 64821
TAMMY DANIELS 207 Birch Leawood MO 64979
JEREMY PALMER 865 Birch Leawood MO 64486
IRENE MILLS 987 Walnut Fairway MO 64367
AARON NICHOLS 168 Apple Raytown KS 64919
JANE GRANT 383 Birch Parkville KS 64191
RANDY KNIGHT 211 Elder Raytown MO 64560
LORI FERGUSON 850 Birch Riverside KS 64113
HOWARD ROSE 437 Peach Olathe KS 64874
RACHEL STONE 236 Peach Raytown KS 64173
EUGENE HAWKINS 992 Oak Parkville KS 64581
MARILYN DUNN 342 Peach Fairway KS 64392
CARLOS PERKINS 80 Maple Olathe MO 64241
ANDREA HUDSON 725 Maple Olathe MO 64006
RUSSELL SPENCER 398 Walnut Raytown MO 64421
KATHRYN GARDNER 869 Cherry Fairway KS 64146
BOBBY STEPHENS 562 Maple Parkville MO 64125
LOUISE PAYNE 508 Birch Fairway KS 64280
VICTOR PIERCE 885 Birch Riverside MO 64832
SARA BERRY 219 Peach Raytown MO 64013
MARTIN MATTHEWS 922 Elder Olathe MO 64648
ANNE ARNOLD 340 Elder Riverside MO 64584
ERNEST WAGNER 878 Apple Olathe MO 64166
JACQUELINE WILLIS 551 Maple Leawood KS 64111
PHILLIP RAY 261 Maple Riverside MO 64401

Actually i meant to attach the input file...
Anyway :D

Now first mistake is the while loop. U cannot read and write the same time. At least not this way :)

So if u change this part:

while(fin >> firstName >> lastName >> houseNumber 
                 >> street >> city >> state >> zipcode1)
    {
        zipcode1 = zipcode;
    changezipcode(zipcode, newzipcode); // Function Call
    fout << setw(10) << firstName << setw(10) << lastName << setw(10) 
          << street << setw(10) << city << setw(10) << state 
          << setw(10) << zipcode << endl;
    }

to this part:

while(fin >> firstName >> lastName >> houseNumber 
                     >> street >> city >> state >> zipcode1)
	{
		zipcode1 = zipcode;
		changezipcode(zipcode, newzipcode); // Function Call
		
	}
	fout << setw(10) << firstName << setw(10) << lastName << setw(10) 
	      << street << setw(10) << city << setw(10) << state 
              << setw(10) << zipcode << endl;

u will see some output now, but still not the whole....
would u like now to give it a try yourself??

EDIT: I figured out why it was saying "uninitialized local variable".

Okay, I've figured something out, I think. Below is the new code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

/*********************************************
Name:           Jarred O'Brien
UMKC Email:     jlo7v3@umkc.edu
Course:         CS 101
Program:        Program 5
Due Date:       10/19/2008
Description:    This program reads the zipcode from an input file and converts the numbers
to actuall words, so "7" becomes "seven".

Inputs:      firstName, lastName, houseNumber, street, city, state, zipcode1
Outputs:        firstName, lastName, houseNumber, street, city, state, zipcode
Algorithm:      still trying to figure out how to word this
*********************************************/

bool checkInputFile(ifstream &fin);  // checks the file input
void changezipcode(int zipcode, string &newzipcode); // this is the function that processes the data from numbers to words

int main()
{
    ifstream fin;
    ofstream fout;

    string firstName;
    string lastName;
    string  city;
    string street;
    string state;
    string newzipcode;
    int houseNumber;
    int zipcodeone;
    int zipcode=0;


    fin.open("program5.txt");
    fout.open("output5.txt");

    if (! checkInputFile(fin))
    {
        exit(2);
    }

    if(fout.fail())
    {
        cerr << "Unable to open output file\n";
        exit(3);
    }
    while (fin >> firstName >> lastName >> houseNumber >> street >> city >> state >> zipcodeone)
    {
   {		
       changezipcode(zipcode, newzipcode); // Function Call
   }

    cout << setw(10) <<firstName << setw(10) << lastName << setw(10) << street << setw(10) << city << setw(10) << state << setw(10) << newzipcode << endl;
    fout << setw(10) << firstName << setw(10) << lastName << setw(10) << street << setw(10) << city << setw(10) << state << setw(10) << newzipcode << endl;
    }

    cout << "Please view the Outputfile: output5.txt\n";

    fin.close();
    fout.close();
    return 0;
}

bool checkInputFile(ifstream &fin)
{
    if(fin.good())
    {
        return true;
    }
    else
    {
        cerr << "Unable to open input file\n";
        return false;
    }
}
void changezipcode(int zipcode, string& newzipcode)
{
    int div=10000;
    string parse;
    int temp;

    while(div>0)
    {
        temp=(zipcode/div);
        switch (temp)
        {
        case '0': parse+="zero"; break;
        case '1': parse+="one"; break;
        case '2': parse+="two"; break;
        case '3': parse+="three"; break;
        case '4': parse+="four"; break;
        case '5': parse+="five"; break;
        case '6': parse+="six"; break;
        case '7': parse+="seven"; break;
        case '8': parse+="eight"; break;
        case '9': parse+="nine"; break;
        }
        zipcode=zipcode-(temp*div);
        div=div/10;
    }
    parse=newzipcode;
    return;
}

for some reason, my function "changezipcode" does NOTHING. Am I using the call right? How can I tell the program that the value of "zipcode" in the function is the value being input by the file?

use couts within the changezipcode function to find out what happens inside there..

Inserting a

cout << newzipcode << endl;

into the changenewzipcode function only made an additional row between each set of data.

The problem is, for some reason the function does not recieve any value for "zipcode", and then it doesn't want to convert "parse" to "newzipcode". Thats what I can tell at least.

Shouldn't thisbe the other way around? parse=newzipcode; so this... newzipcode=parse; Maybe i missed something

Chris

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.