pig latin program...command line argument

I have a simple program that takes a word or line of text and will convert it to pig latin. BUT I need it to take command line argument, instead of user input. I didn't include the functions, here, because they work, the program works as is, and I dont think that I need to change the functions to change my program to command line arguments.
If the line of text is withing " ", is it considered just 1 argument? If so, For my program, would there only be 2 argc(the filename and the "line of text"?
Also, how would I go about implementing this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <iostream>

using namespace std;

//functions
bool is_vowel(char n);
void vowelFirst(string convert);
void consFirst(char begin, string end);
void pigLatin (string sentence);

int main()// I need to convert my program to take command line argument instead....int main(int argc, char *argv[])
{

    char ch;
    string text;
    string firstLetter;
    string sentence;

     cout << "Please enter a word or string of text, I will convert it to pig latin"<< endl;
    getline(cin,sentence);//input a line of words
    cout <<endl;

{
pigLatin(sentence); // output of the new sentence in Pig Latin
}
return 0;
}
There is a nice article on how to parse command line arguments here
http://www.cplusplus.com/articles/DEN36Up4/
The following program accepts a command line argument, which is then opened by the ifstream object, and its contents then assigned to a string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>

int main(int argc, char** argv) {
	SetConsoleTitleA("Input File Stream");
	if(argc!=2) {
		//error I guess
		return 1;
	}
	std::ifstream input(argv[1], std::ios::binary);

	std::string string((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());

	std::cout << string << std::endl;

	std::cin.get();
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char **argv) {
	/*	argc = argument count
		argv = argument vector	*/
	if ( argc == 1 ) {
		printf("Usage: %s strings...\n", *argv );
		return -1;
	}
	
	while (--argc) pigLatin(*++argv);
	return 0;
}
Last edited on
Okay Thanks. I did edit my code base on Smac98 post. I still have my code for the user input part.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
     int main(int argc, char **argv)
{
  
    string sentence;
 
    
    /*	argc = argument count
		argv = argument vector	*/
	if ( argc !=2 ) 
    {
		cout << "Usage: %s strings..." << *argv << endl;
		return 1;
	}
	
	while (--argc) pigLatin(*++argv);
	return 0;


cout << "Please enter a word or string of text, I will convert it to pig latin"<< endl;   // not needed
getline(cin,sentence);//input a line of words  //need to change
cout <<endl;

{
pigLatin(sentence); // output of the new sentence in Pig Latin

}
return 0;
}


I know that is not needed, but how do I change that part to be of command line argument type? xismn suggested fstream. We haven't learned anything about this. can I somehow do it using the following:
1
2
3
4
5
#include <iostream>
#include <string>         
#include <cstdlib>         
#include <cstring>         
#include <cctype>  


example:
1
2
3
string sentence = argv[2];
       cout << "The english text is: " << sentence << endl;
       cout<< "The pig latin text is: "<< pigLatin(sentence) << endl;


Last edited on
Your question does not make sense. What are you trying to do exactly?
He is saying he is only allowed to use those includes and not fstream
Yes, Sorry if that was confusing. I am trying to only use the "#includes" that I have listed.( if it is possible)

and the example is what I would use:

set sentence = to argv[2] ... in order to bring "......string of text....." from command line argument into the program (so to speak).

And from their have the program do all of the manipulations to the words.

Is this correct or am I going about it wrong?

Thanks.
So I got the program working now.

But how can I error check for non-letters or non words

ex. (I want to run a cout for these things that give a usage statement to the user, words or sentences only.

./piglatin @
result:

@ay

or

./piglatin 457

result:

457ay

here is the current code for the int main (didnt include the functions, because there are no problems there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include<iostream>
  #include<string>
  #include<cstdlib>
  #include<cstring>
  #include<cctype>
 
  using namespace std;
 
  //functions
  bool is_vowel(char n);
  void vowelFirst(string vowelWord);
  void consFirst(char beginning, string ending);
  void pigLatin(string sentence);
 
  int main(int argc, char* argv[])
  {
          string sentence;
 
          if(argc !=2)
          {
                  cout<< "Usage: ./piglatin [string of text]" << endl;
                  return 0;
          }
          while(--argc)pigLatin(*++argv);
          return 0;
 
  /*      cout<< "Please enter a word or string of text, I will convert it to     pig latin. "<< endl;
          getline(cin, sentence);  //gets the english line of text
          cout<< endl;*/
 
          sentence = argv[2];
 
 
          {
                  pigLatin(sentence); //calls the pigLatin function
          }
 
          return 0;
  }
Last edited on
What is line 31 to 38 supposed to be doing?
I hope you know how functions work; after you return a value from a function, anything below the return the statement is not executed unless it was a recursive call. You are not doing a recursive call anywhere and yet you have an assignment (which will most likely result in a segfault), 2 curly brackets without a condition and another return statement below the first return statement.

Again this code makes no sense. Lines 1-25 is already the entire program
Okay, I see what you are saying. I deleted line 27-36.

But still is there a way to do error checking for non words.

Say is somebody entered

./piglatin "78"

or ./piglatin "%&^"

Thanks.
By non-words I assume you mean when the string contains numbers or symbols?

http://www.cplusplus.com/reference/cctype/isalpha/
Using other locales, an alphabetic character is a character for which isupper or islower would return true, or another character explicitly considered alphabetic by the locale (in this case, the character cannot be iscntrl, isdigit, ispunct or isspace).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* isalpha example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="C++";
  while (str[i])
  {
    if (isalpha(str[i])) printf ("character %c is alphabetic\n",str[i]);
    else printf ("character %c is not alphabetic\n",str[i]);
    i++;
  }
  return 0;
}


Based on this description, I'm not sure if using this would work, because wouldn't a space be a non letter?
because spaces are okay. now, that I'm thinking about it ...characters should be permitted but ignored.....
ex.

./piglatin "I'm going to the store."

./piglatin "It's 8 o'clock."

This sentence should be allowed to be translated. thereby allowing characters.

but what about a string off nonsensical characters or string of numbers.

Is it possible to errorcheck for that whille still allowing examples like above?
I guess rather that not allowing characters....

Can I allow only certain characters ( ', ., ?, !, &, : )?

and if so how?
Topic archived. No new replies allowed.