locked
String and integer manipulation within a function RRS feed

  • Question

  • open and read from an input file named prog4.dat.

    The file will have 15 integers(greater than or equal to 2).

    Following that will be 10 words(strings of lowercase characters with no spaces) per line.

    For the 15 integers, your code will determine whether or not the integer is prime.

    For the 10 strings, the string will be converted to a "secret code." 

    **Partial sample input file:**

    27

    19

    dog

    elephant

    **output**

    @@@@@@@@@@@@@ test @@@@@@@@@@@@@@@@

    27 is not prime.

    19 is prime.

    c0f

    3k3og9ms

    @@@@@@@@@@@@@ test @@@@@@@@@@@@@@@@

     - One function will determine whether or not a number is prime.
     - Another function (the one that produces the "secret code") will return a string and take one value parameter of type string.  
     - The other function will take 2 parameters, a value parameter of type string and a reference parameter of type ofstream.  This function is a void function and will simply print a nice border including the string that was passed in.

    **Secret code instructions** --For each string passed in, convert each consonant to the letter immediately beforeit inthe alphabet.  

    For vowels, use these rules:

    a -> 9  e -> 3  i -> 1  o -> 0  u -> 4
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    
    using namespace std;
    int checkPrimeNumber(int);
    string secretCode(string);
    void border(ofstream& outfile);
    
    
    int main()
    {
    	ofstream outfile;
    	ifstream infile;
    	infile.open("prog4.txt");
    	outfile.open("outfile.txt");
    
    	int n;
    
    	border;
    
    	infile >> n;
    
    	if (checkPrimeNumber(n) == 0)
    		outfile << n << " is a prime number.";
    	else
    		cout << n << " is not a prime number.";
    	return 0;
    }
    
    string secretCode(string s)
    {
    
    
    }
    
    
    int checkPrimeNumber(int n)
    {
    	bool flag = false;
    
    	for (int i = 2; i <= n / 2; ++i)
    	{
    		if (n%i == 0)
    		{
    			flag = true;
    			break;
    		}
    	}
    	return flag;
    }
    
    void border(ofstream& outfile) {
    	outfile << "@@@@@@ test @@@@@@\n";
    }
    
    


            
    I want to know How to do the secret code part and the ofstream refference part
    • Edited by tes64dk Monday, November 21, 2016 1:24 AM finished one more problem
    Monday, November 21, 2016 1:00 AM

All replies

  • I guess noone will do your homework here.

    If you have specific questions on creating the "secretcode" function please post your code and ask specific questions.

    At all it is not really difficuilt. You only have to do 2 different things with your string. In the first step I'd replace the vowels with the numbers like the rule said. In the second step I'd decrement the value of each character if it is not a number.


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Monday, November 21, 2016 7:53 AM
    • Proposed as answer by Bordon Monday, November 21, 2016 7:53 AM
    Monday, November 21, 2016 7:50 AM
  • Hi bluezapfusion,

    thanks for posting here.

    >>I want to know How to do the secret code part and the ofstream refference part

    For your case, I have wrote a sample code for you. You could refer to.

    For the secretCode method:

    string secretCode(string s)
    {
     string sResult = "";
     string sTemp = "";
     int nTemp = 0;
     for (int i = 0; i < s.length(); i++)
     {
      nTemp = (int)(s[i]);
      switch (nTemp)
      {
      case 97:
       sTemp = "9";
       break;
      case 101:
       sTemp = "3";
       break;
      case 105:
       sTemp = "1";
       break;
      case 111:
       sTemp = "0";
       break;
      case 117:
       sTemp = "4";
       break;
      default:
       sTemp = s[i];
       break;
      }
      sResult.append(sTemp);
     } 
     return sResult;
    }
    

    For the ofstream reference part.

    infile >> nIn;
    string sSecret = secretCode(nIn);
    outfile << sSecret << endl;

    The result will be this

    Hope this could be help of you.

    Best Regards,
    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Baron Bi Monday, November 21, 2016 8:14 AM
    Monday, November 21, 2016 8:14 AM
  • hi could you please show me how you recognized an integer from a string thanks
    Monday, November 21, 2016 6:16 PM
  • whats nIn here?
    Monday, November 21, 2016 6:19 PM
  • Hi tes64dk,

    >>hi could you please show me how you recognized an integer from a string thanks

    You could use isdigit method. Or try like this.

    bool is_number(const std::string& s)
    {
        std::string::const_iterator it = s.begin();
        while (it != s.end() && std::isdigit(*it)) ++it;
        return !s.empty() && it == s.end();
    }

    Hope this could be help of you.

    Best Regards,
    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Baron Bi Monday, November 28, 2016 1:57 AM
    Tuesday, November 22, 2016 3:09 AM
  • >>whats nIn here?

    I just write a simple code to fix your problems especially for your situation, two numbers and two strings. So just forget about the variable names.

    Best Regards,

    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 22, 2016 3:11 AM
  • hi could you please show me how you recognized an integer from a string thanks

    The instructions for the program specify that the strings will contain lower case characters.  Therefore, you will never find a digit, an upper case character, or a punctuation character in any of the strings.

    So why do you think you need to recognize an integer? 

    Wednesday, November 23, 2016 3:15 PM