Thread: Help Newbie Please

  1. #1
    Unregistered
    Guest

    Talking Help Newbie Please

    Hello guys..........Ive been programming for about a week now and I need some help.



    #include <iostream.h>




    Code:
    int main()
    
    
    {
        char state[10];
        char address[10];
        char name[10];
        int age;
        char choice[4] ;
    	
    	do{
    	cout << "Please enter in the correct information\n\n\n";
    	
    	cout << "State: ";
    	cin >> state;
    
    	cout << "Address: ";
    	cin >> address;
    
    	cout << "Name: ";
    	cin >> name;
    
    	cout << "Age: ";
    	cin >> age;
    	cout << "\n\n";
    
    
    
    	cout << "State: " << state << endl;
    	cout << "Address: " << address << endl;
    	cout << "Name: " << name << endl;
    	cout << "Age: " << age << endl;
    	cout << "Is this information correct? Yes or No: ";
    	cin >> choice;
    	}while(choice == "yes");
    	
    
    	
    	return 0;
    }


    Looks correct doesnt it? But if you compile it with Microsoft Visual C++ it lets you enter the information for State and Address but not for name and age. Anyone know the problem? Also I want to loop untill the user types in yes. Can anyone help me with that as well. Thanks in advance.
    -Jeremy

  2. #2
    MrWizard
    Guest
    Try using cin.getline( ). For all the parameters look in MSDN.

    http://msdn.microsoft.com/library/de...3a.getline.asp

    As far as looping goes. Look into a while loop. Add a simple boolean value in the main function. and then check the condition in the while loop. Here is a simple example.
    Code:
    int main( void )
    {
       unsigned int value = 0;
       bool bDone = false;
       
       while( bDone == false )
       {
          cout << "Enter a number: ";
          cin >> value;
          if( value == 1 )  // some random number for example
          {
             bDone = true;
          }
          cout << endl;
       }
    
       return 0;
    }
    or alternatively without the boolean value...
    Code:
    int main( void )
    {
       unsigned int value = 0;
       while( value != 1 ) // loop until this is false
       {
          cout << "Enter a number: ";
          cin >> value;
          cout << endl;
       }
    
       return 0;
    }
    Again, I just wrote those real quick, they may or may not compile. Hopefully that will get you on the right track though. Have fun above all.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    cin will stop inputting into a char array when it reads a white space character. So if you enter two words in response to one question, the first word is taken, the second word will remain in the input buffer until the next call of cin. As per MrWizard's comment, try cin.getline().

    And you cannot do this:
    >while(choice == "yes");
    To compare char arrays you should use strcmp().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest
    Originally posted by Hammer
    cin will stop inputting into a char array when it reads a white space character. So if you enter two words in response to one question, the first word is taken, the second word will remain in the input buffer until the next call of cin. As per MrWizard's comment, try cin.getline().

    And you cannot do this:
    >while(choice == "yes");
    To compare char arrays you should use strcmp().
    Please explain? White space charecter? And how do I use strcmp().

  5. #5
    minesweeper
    Guest
    strcmp(string_a,string_b);

    It compares the two strings to see if they are identical. If they are then the function returns 0. Returns a positive value if string_a is greater then string_b and a negative value if the other way round.

    And you will need to use string.h.

  6. #6
    Unregistered
    Guest

    Smile

    Thanks guys for all your help, they werent wrong when they told me to come here if I needed help.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    a little more involved question

    let's say that i wanted to take user input, that was on separate lines, and wanted to store that input in a single array. how could that be done?

    for example, i have a mailing address that i wanted stored as a single array, but the user inputs it all on three different lines:

    john smith
    123 green street
    nowhere, usa 12345

    how could the "123 green street" and "nowhere, usa 12345" be appended to a single array?
    eat, drink and be merry. for tomorrow: we party.

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You can use strcat(). It concatenates two strings together. So you will need to do it a couple of times. First with the name and address. Then with the result from the previous operation with your state/country. However you may want to insert spaces or any delimiter you need in order to separate the fields in the array.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    10
    Have you tried making a 3 x whatever array?

    int SomeArray[3][12]
    Last edited by Universe; 08-12-2002 at 05:42 PM.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    hmm, not there yet.

    i got the strcat, but to make life even more exciting, let's say that i dont know how the user will choose to input the information.

    john smith
    123 green way
    nowhere, usa 12345

    or

    john smith 123 green way nowhere usa, 12345

    i guess what im looking for is a way to skip the white space and store everything into a single character array, if you dont know how many different *words* there are in the input (perhaps something formatted around null-termination)...
    eat, drink and be merry. for tomorrow: we party.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    no no, i recant.

    "i should learn to think before speaking"

    okay, i think i got somewhere. i wouldn't be able to format something around the null-termination, because i press <enter> on every line, which is what separates each line of input (forgive my grammar and redundancy here). rather, i need to format my own loop and select a delimiter for the user to input at the end of their input.

    if i could only make the above a more concise thought, i'd have so much more success.

    *sigh*
    eat, drink and be merry. for tomorrow: we party.

  12. #12
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Why do you need to store all 3 info into 1 array?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  13. #13
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    well

    of course, i am programming for a class that finishes in hmm....about 38 hours now, during which time i need to finish one project and begin(as well as complete) four assignments.

    i am procrastination: personified.

    what i am aiming to do, is create a linked list of "records". each record is defined as a "name" character array, "address" character array, year of birth int, and "phone" character array. each of these arrays are populated by information inputted by the user.

    of course, i am only beginning to understand what i am doing. but understanding nonetheless.
    eat, drink and be merry. for tomorrow: we party.

  14. #14
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Then I suggest you create a structure.
    Hope this gets you on the right track:

    Code:
    typedef struct record
    {
       char name[SIZE];
       char address[SIZE];
       int yearOfBirth;
       char phone[SIZE];
       struct record *next;
    }RECORDS;
    
    RECORDS *head;
    RECORDS *current;
    ...
    head = malloc(sizeof(RECORDS));
    current = head;
    current->next = NULL;
    // populate your info
    ...
    This will build your linked list of RECORDS. All you need to do is write functions that populate and build the list. Then you can do whatever you need to do with them.
    Last edited by Cshot; 08-12-2002 at 06:16 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  15. #15
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    instructors now days...

    the complicated stuff, i have a minority kind of understanding on...like the concept of a linked list...of course, it would be the implementation of such that's got me pulling my hair....

    the trivial little things like the use of a delimiter...blah blah.

    caffeine is my friend.
    eat, drink and be merry. for tomorrow: we party.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM