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.

Recommended Answers

All 4 Replies

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';
    }
  }
}

>>compare and index or argv to i.
I suspect you misunderstood your assignment.

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

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';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.