0

how can i convert an array of char to char?

this what i want to do

char *argv[];

char i


compare and index or argv to i.

3
Contributors
4
Replies
7
Views
8 Years
Discussion Span
Last Post by Radical Edward
0

An array of char cannot be converted to char unless it is an array of 1. The reason is that one cannot fit more than one char value into a single char variable. You can process each char individually though. To do that, use two nested loops, one for each argument string and one for the characters in each string:

#include <iostream>

int main(int argc, char *argv[])
{
  // Start at 1 to skip the program name
  for (int i = 1; i < argc; ++i) {
    for (int j = 0; argv[i][j] != '\0'; ++j) {
      char ch = argv[i][j];

      std::cout << ch << '\n';
    }
  }
}
0

i dont think i have i need to check if the checking for U command or I!!!

0

Ed's just guessing now, but are you trying to parse command line options? Something like this?

#include <iostream>
#include <stdexcept>

// Notes:
//   Accepted commands
//     -I <single arg>
//     -U <single arg>
//
//  example: myprog -I woohoo -U "Ed rules!"

int main(int argc, char *argv[]) try
{
  // Start at 1 to skip the program name
  for (int i = 1; i < argc; ++i) {
    // Only options are allowed
    // Options consist of - and at least one more character
    if (argv[i][0] != '-' || argv[i][1] == '\0')
      throw std::runtime_error("Bad format");

    // Handle the option
    switch (argv[i][1]) {
      case 'I':
        // Be sure to skip the option's argument
        std::cout << "Reading I: " << argv[++i] << '\n';
        break;
      case 'U':
        // Be sure to skip the option's argument
        std::cout << "Reading U: " << argv[++i] << '\n';
        break;
    }
  }
}
catch (const std::exception& ex) {
  std::cerr << ex.what() << '\n';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.