Compiler error C2374 - Multiple initialization

This is a discussion on Compiler error C2374 - Multiple initialization within the C++ Programming forums, part of the General Programming Boards category; Well, I thought I had this program working but now I'm getting the above referenced compiler error. The program is ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    33

    Compiler error C2374 - Multiple initialization

    Well, I thought I had this program working but now I'm getting the above referenced compiler error. The program is just a basic user interface. It is for a classwork assignment.

    The program is to accept user information as a string, convert it (if needed) to either the int or double variable, and then display the result. I'm using stringstream convert to make the change between types, but I'm not sure if I'm using it right (that might be what's causing the error, I'm not sure). Line 36-37 generates the error.

    Can anyone recommend a way to fix this?
    Thanks.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    using namespace std;
    
    string input, name;
    int age;
    double mileage;
    
    void DisplayApplicationInformation();
    void DisplayDivider(string outputTitle);
    string GetInput(string inputType);
    int TerminateApplication();
    
    int main()
    {
        DisplayApplicationInformation();
        cout << endl;
        DisplayDivider("Start Program");
        cout << endl;
        
        DisplayDivider("Get Name");
        name = GetInput("Your Name");
        cout << "Your name is: " << name << endl;
        cout << endl;
    
        DisplayDivider("Get Age");
        input = GetInput("Your Age");
        stringstream convert(input);
        if ( ! (convert >> age) ) age = 0;
        cout << "Your age is " << age << endl;
        cout << endl;
        
        cout << fixed << showpoint << setprecision(2) << endl;
        DisplayDivider("Get Mileage");
        input = GetInput("Your Gas Mileage");
        stringstream convert(input);
        if ( ! (convert >> mileage) ) mileage = 0;
        cout << "Your car's MPG is: " << mileage << endl;
        cout << endl;
    
        TerminateApplication();
    }
    
    void DisplayApplicationInformation() //provides the user with basic program information
    {
        cout << "Welcome to the Basic User Interface Program" << endl;
        cout << "CIS247C, Week 1 Lab" << endl;
        cout << "Name: John Worley" << endl;
        cout << "This program accepts user input as a string," << endl; 
        cout <<    "then makes the appropriate data conversion." << endl;
        cout << "Finally the program will display the user input," << endl;
        cout << "as a string, integer, or double number as needed." << endl;
    }
    
    void DisplayDivider(string outputTitle) //output separator
    {
        cout <<"**********" << outputTitle << "**********" << endl;
    }
    
    string GetInput(string inputType) //general function that prompts user for information then returns info as string
    {
        string strInput;
        cout << "Enter " << inputType << endl;
        cin >> strInput;
    
        return strInput;
    }
    
    int TerminateApplication() //program termination message and end program
    {
        cout << "Thank you for using the Basic User Interface program." << endl;
        system("pause");
        return 0;
    }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You are declaring convert twice.
    At line 37, just use the str member function.
    Code:
    convert.str(input);
    Last edited by manasij7479; 11-01-2013 at 12:56 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    While manasij7479's suggestion should work, you may need to call convert.clear() before that in order to clear the error state of the stringstream.

    By the way, those global variables should be local to the main function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    33
    Thanks for the replies.

    I changed these things around and got it to work. There is just one last quirk that I'm curious about. When I run the program and it asks for my "name" input, if I use just a first name like "John", then the program works just fine. However if I use a first and last name, like "John Smith", then the program only displays the first name, "John", and then it skips over the prompt for "age" input, displays my age as "0", and then asks for the "mileage" input.

    Any thoughts on what is causing this?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    using namespace std;
    
    void DisplayApplicationInformation();
    void DisplayDivider(string outputTitle);
    string GetInput(string inputType);
    int TerminateApplication();
    
    int main()
    {
        string input, name;
        int age;
        double mileage;
    
        DisplayApplicationInformation();
        cout << endl;
        DisplayDivider("Start Program");
        cout << endl;
        
        DisplayDivider("Get Name");
        name = GetInput("Your Name");
        cout << "Your name is: " << name << endl;
        cout << endl;
    
        DisplayDivider("Get Age");
        input = GetInput("Your Age");
        stringstream convert(input);
        if ( ! (convert >> age) ) age = 0;
        cout << "Your age is " << age << endl;
        cout << endl;
        convert.clear();
        
        cout << fixed << showpoint << setprecision(2) << endl;
        DisplayDivider("Get Mileage");
        input = GetInput("Your Gas Mileage");
        convert.str(input);
        if ( ! (convert >> mileage) ) mileage = 0;
        cout << "Your car's MPG is: " << mileage << endl;
        cout << endl;
    
        TerminateApplication();
    }
    
    void DisplayApplicationInformation() //provides the user with basic program information
    {
        cout << "Welcome to the Basic User Interface Program" << endl;
        cout << "CIS247C, Week 1 Lab" << endl;
        cout << "Name: John Worley" << endl;
        cout << "This program accepts user input as a string," << endl; 
        cout <<    "then makes the appropriate data conversion." << endl;
        cout << "Finally the program will display the user input," << endl;
        cout << "as a string, integer, or double number as needed." << endl;
    }
    
    void DisplayDivider(string outputTitle) //output separator
    {
        cout <<"**********" << outputTitle << "**********" << endl;
    }
    
    string GetInput(string inputType) //general function that prompts user for information then returns info as string
    {
        string strInput;
        cout << "Enter " << inputType << endl;
        cin >> strInput;
    
        return strInput;
    }
    
    int TerminateApplication() //program termination message and end program
    {
        cout << "Thank you for using the Basic User Interface program." << endl;
        system("pause");
        return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by worl4125
    When I run the program and it asks for my "name" input, if I use just a first name like "John", then the program works just fine. However if I use a first and last name, like "John Smith", then the program only displays the first name, "John", and then it skips over the prompt for "age" input, displays my age as "0", and then asks for the "mileage" input.

    Any thoughts on what is causing this?
    That is because you are using operator>> to read into a string. By default, operator>> will read until whitespace is reached. Hence, if you enter two words, you only get the first one read into the string. The skipping over the prompt for the age isn't actually skipping: rather, there was the attempt read "Smith" into the int, which failed.

    Instead of using operator>>, use the non-member getline function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    33
    Quote Originally Posted by laserlight View Post
    That is because you are using operator>> to read into a string. By default, operator>> will read until whitespace is reached. Hence, if you enter two words, you only get the first one read into the string. The skipping over the prompt for the age isn't actually skipping: rather, there was the attempt read "Smith" into the int, which failed.

    Instead of using operator>>, use the non-member getline function.
    Thanks. I understand now. I completely forgot about using getline over cin when dealing with strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'invalid initialization...' error.
    By Absurd in forum C++ Programming
    Replies: 9
    Last Post: 05-22-2013, 12:42 PM
  2. Initialization Error..? Help Please
    By Matt Hintzke in forum C Programming
    Replies: 8
    Last Post: 02-02-2012, 01:17 PM
  3. array initialization error
    By FlyingShoes12 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2009, 10:07 AM
  4. SDL Initialization Error...
    By Comrade_Yeti in forum C++ Programming
    Replies: 0
    Last Post: 05-16-2005, 05:43 PM
  5. variable initialization error...
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2005, 12:01 AM

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