CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Posts
    4

    Make program loop untill null value is passed.

    Hi,

    i've had to write this program below to take input in the form of "ONE-HUNDRED" and output "100", only i don't want it to exit straight away. I want it to keep taking inputs untill a null value is passed.

    I tried using a do while loop but it doesn't take in inputs after the first one and keeps printing the current output to the screen.

    Here is what i have so far.

    #include <iostream>
    #include <string.h>
    using namespace std;

    int main()
    {
    char number[80];
    char *str = NULL;
    int output = 0;

    cout << "Student ID 4095983" << endl;

    cin.get( number, 79 );
    str = strtok( number, " -" );

    while ( str != NULL ) {

    if( strcmp(str, "ZERO") == 0) {
    output += 0;
    }
    else if( strcmp(str, "ONE") == 0) {
    output += 1;
    }
    else if( strcmp(str, "TWO") == 0) {
    output += 2;
    }
    else if( strcmp(str, "THREE") == 0) {
    output += 3;
    }
    else if( strcmp(str, "FOUR") == 0) {
    output += 4;
    }
    else if( strcmp(str, "FIVE") == 0) {
    output += 5;
    }
    else if( strcmp(str, "SIX") == 0) {
    output += 6;
    }
    else if( strcmp(str, "SEVEN") == 0) {
    output += 7;
    }
    else if( strcmp(str, "EIGHT") == 0) {
    output += 8;
    }
    else if( strcmp(str, "NINE") == 0) {
    output += 9;
    }
    else if( strcmp(str, "TEN") == 0) {
    output += 10;
    }
    else if( strcmp(str, "ELEVEN") == 0) {
    output += 11;
    }
    else if( strcmp(str, "TWELVE") == 0) {
    output += 12;
    }
    else if( strcmp(str, "THIRTEEN") == 0) {
    output += 13;
    }
    else if( strcmp(str, "FOURTEEN") == 0) {
    output += 14;
    }
    else if( strcmp(str, "FIFTEEN") == 0) {
    output += 15;
    }
    else if( strcmp(str, "SIXTEEN") == 0) {
    output += 16;
    }
    else if( strcmp(str, "SEVENTEEN") == 0) {
    output += 17;
    }
    else if( strcmp(str, "EIGHTEEN") == 0) {
    output += 18;
    }
    else if( strcmp(str, "NINETEEN") == 0) {
    output += 19;
    }
    else if( strcmp(str, "TWENTY") == 0) {
    output += 20;
    }
    else if( strcmp(str, "THIRTY") == 0) {
    output += 30;
    }
    else if( strcmp(str, "FORTY") == 0) {
    output += 40;
    }
    else if( strcmp(str, "FIFTY") == 0) {
    output += 50;
    }
    else if( strcmp(str, "SIXTY") == 0) {
    output += 60;
    }
    else if( strcmp(str, "SEVENTY") == 0) {
    output += 70;
    }
    else if( strcmp(str, "EIGHTY") == 0) {
    output += 80;
    }
    else if( strcmp(str, "NINETY") == 0) {
    output += 90;
    }
    else if( strcmp(str, "HUNDRED") == 0) {
    output *= 100;
    }
    else if( strcmp(str, "THOUSAND") == 0) {
    output *= 1000;
    }
    else if( strcmp(str, "MILLION") == 0) {
    output *= 1000000;
    }
    str = strtok( NULL, " -" );
    }
    cout << "Conversion to integer is: " << output << endl;

    }

    Any help appreciated,
    thanks

  2. #2
    Join Date
    May 2000
    Location
    Scotland, Livingston.
    Posts
    728

    Re: Make program loop untill null value is passed.

    Hint: you need two loops, one inside the other.
    Dave Mclelland.

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Make program loop untill null value is passed.

    Yet another hint.
    You have to get rid of a remaining '\n' from a previous iteration before using cin.get().

  5. #5
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Make program loop untill null value is passed.

    One more hint: use std::map<string,whatever> instead of that long list of if(strcmp(...)==0).
    "Programs must be written for people to read, and only incidentally for machines to execute."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured