Errors in program regarding strings.

This is a discussion on Errors in program regarding strings. within the C++ Programming forums, part of the General Programming Boards category; Hello, Im very new to c++ and was wondering if you could give me some much needed help: My problem ...

  1. #1
    Unregistered
    Guest

    Errors in program regarding strings.

    Hello,
    Im very new to c++ and was wondering if you could give me some much needed help:
    My problem is quite simple, i have two strings in my program:
    Code:
    char name[50];
    char filepath[50];
    when i come to get a user inputed value for name using this:
    Code:
    cin.getline(name, 50, '\n');
    it works fine.

    When i try and get the same input for filepath using this:
    Code:
    cin.getline(filepath, 50, '\n');
    Everything complies ok, when i run it, "name" works fine, but when i get to "filepath" it doesn't wait forinput, it just continues on and exits.

    Here is my code:

    Code:
    // simpcalc.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"  
    #include "math.h"
    #include "iostream.h"
    #include "stdlib.h" 
    #include "fstream.h"
    
    int main()
    {
      char name[50];
      char filepath[50];
      int age;
      int tel;  
      
    cout<<"Please enter the following details about yourself:" <<endl;
    cout<<"Name: ";
    cin.getline(name, 50, '\n');
    cout<<"Age: ";
    cin>>age;
    cout<<"Telephone Number: ";
    cin>>tel;
    cout<<"What would you like the file where your information is recorded to be called?";
    cin.getline(filepath, 50, '\n');
    
      
    ofstream yourinfo(filepath);
    yourinfo<<"This is the information you gave us on yourself:" <<endl;
    yourinfo<<"Your name is: "<<name <<endl ;
    yourinfo<<"Your age is: "<<age <<endl;
    yourinfo<<"Your telephone number is: " <<tel <<endl;   
    yourinfo.close();                           
    
    cout<<"Your information has been recorded. Thank you.";
    exit(0);
    
    return 0;
    }
    Im sorry that its not commented, by i usually do that at the end. If you run the code, hopefully you'll see the problem and be able to help.
    ANY help is much appreciated.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    7
    Please help, im a member now!


  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    Change
    cin>>tel;

    To
    cin>>tel;
    cin.ignore(1000,'\n');

    Same problem as mixing scanf and fgets in C

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    7
    Thanks alot, that is very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Errors with program
    By nizbit in forum C Programming
    Replies: 37
    Last Post: 12-19-2004, 08:56 PM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 09:40 AM

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