Need some help with integer strings

This is a discussion on Need some help with integer strings within the C++ Programming forums, part of the General Programming Boards category; Hi, My program must read 10 integers(whole numbers) and store them as a single string. Iv'e gotten this far, can ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Need some help with integer strings

    Hi,

    My program must read 10 integers(whole numbers) and store them as a single string.

    Iv'e gotten this far, can anyone give me some help what I should do to complete it.

    Code:
    //---------------------------------------------------------------------------
    #include<conio>
    #include<iostream>
    #include<cstring>
    #pragma hdrstop
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    
    {
       char string[20] = "";
       char stringNumber[4];
       int number;
    
       for ( int i = 0; i < 10; i++ )
       {
    
    
       }
       cout << string ;
    
       getch();
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    This looks like just some template, like you havn't done a single thing. Read your C++ book, look up User input/output. Then come back when you put some effort in.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    How would this compile?

    Code:
    //---------------------------------------------------------------------------
    #include<conio>
    #include<iostream>
    #include<cstring>
    #pragma hdrstop
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
       char string[40] = "";
       char stringNumber[4];
       int number;
    
       for ( int i = 0; i < 10; i++ )
       {
          cout << "Please input a number " << i+1 << endl;
          cin >> number;
          strcat( string, itoa( number, stringNumber, 10 ) ) ;
       }
       cout << string ;
       getch();
            return 0;
    }
    //---------------------------------------------------------------------------

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Why don't you compile it and find out?

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    ew

    let me spare you from hours of self-torture.

    Code:
    #include <iostream>
    #include <string.h>
    #include <conio.h> //windows
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       char str[40];
       char stringNumber[8];
       int number,i;
    	strcpy(str,"");
       for ( i = 0; i < 10; i++ )
       {
          strcpy(stringNumber,"");
          cout << "Please input a number " << i+1 << endl;
          cin >> number;
          sprintf(stringNumber,"%d",number);
    	  strcat(stringNumber,"\0");
          strcat(str,stringNumber);
       }
       cout << str;
       getch();
       return 0;
    }
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  3. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  4. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 12:16 PM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21