Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Command Line Args Errors
New Posts  All Forums:Forum Nav:

Command Line Args Errors

post #1 of 3
Thread Starter 
Code:
int main(int argc, char* argv[]) 
{
    string m, m2, c1, c2;

    if (argc == 1)
    {
        cout << "Enter m or m1: ";
        cin >> m;

        cout << "Enter m2: ";
        cin >> m2;

        cout << "Enter c1: ";
        cin >> c1;

        cout << "Enter c2: ";
        cin >> c2;
    }
    else if (argc == 2)
    {
        m = argv[1];

        cout << "Enter m2: ";
        cin >> m2;

        cout << "Enter c1: ";
        cin >> c1;

        cout << "Enter c2: ";
        cin >> c2;
    }
    else if (argc == 3)
    {
        m = argv[1];
        m2 = argv[2];
        c1 = argv[3];

        cout << "Enter c2: ";
        cin >> c2;
    }
    else
    {
        m = argv[1];
        m2 = argv[2];
        c1 = argv[3];
        c2 = argv[4];
    }

    return 0;
}

No errors in linux when I either input programName and it prompts for the inputs, programName value1, or when I input programName and all the parameters at once.

I got segmentation fault when I do
programName value1 value2 or programName value1 value2 value3

What is wrong? Thanks!
    
CPUGraphicsRAMHard Drive
Intel Core i7 4870HQ 2.5GHz AMD Radeon R9 M370X 2048 MB 16 GB 1600 MHz DDR3 1TB SSD 
OS
macOS Sierra 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel Core i7 4870HQ 2.5GHz AMD Radeon R9 M370X 2048 MB 16 GB 1600 MHz DDR3 1TB SSD 
OS
macOS Sierra 
  hide details  
Reply
post #2 of 3
You were trying to access memory that wasn't allocated, hence the segmentation fault. See the changes...
Code:
#include <iostream>
#include <string>

using namespace std;


int main(int argc, char* argv[]) 
{
    string m, m2, c1, c2;


    if (argc == 1)
    {
        cout << "Enter m or m1: ";
        cin >> m;

        cout << "Enter m2: ";
        cin >> m2;

        cout << "Enter c1: ";
        cin >> c1;

        cout << "Enter c2: ";
        cin >> c2;
    }
    else if (argc == 2)
    {
        m = argv[1];

        cout << "Enter m2: ";
        cin >> m2;

        cout << "Enter c1: ";
        cin >> c1;

        cout << "Enter c2: ";
        cin >> c2;
    }
    else if (argc == 3)
    {
        m = argv[1];
        m2 = argv[2];

        cout << "Enter c1: ";
        cin >> c1;
        

        cout << "Enter c2: ";
        cin >> c2;
    }
    else if (argc == 4)
    {
        m = argv[1];
        m2 = argv[2];
        c1 = argv[3];

        cout << "Enter c2: ";
        cin >> c2;
    }
     else{
        
        m = argv[1];
        m2 = argv[2];
        c1 = argv[3];
        c2 = argv[4];
        
      }

    return 0;
}

Edited by surfbumb - 4/22/12 at 12:14pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #3 of 3
Thread Starter 
Lol! Oops, forgot about the argv == 4. I used the code from my other program and made some changes. Forgot this has one more prompt. Thanks!
    
CPUGraphicsRAMHard Drive
Intel Core i7 4870HQ 2.5GHz AMD Radeon R9 M370X 2048 MB 16 GB 1600 MHz DDR3 1TB SSD 
OS
macOS Sierra 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel Core i7 4870HQ 2.5GHz AMD Radeon R9 M370X 2048 MB 16 GB 1600 MHz DDR3 1TB SSD 
OS
macOS Sierra 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Command Line Args Errors