Author Topic: C++ Help  (Read 929 times)

Offline leadtail

  • (SOL) Navy Members
  • not so noob
  • ***
  • Posts: 131
  • Kudos: +9/-7
  • noob
    • View Profile
C++ Help
« on: October 30, 2016, 09:06:55 am »
The youngest has a C++ program the she is having trouble with. Here is the problem for her class.

Write a program that will read lines of characters from a file. Each line will end with the @ symbol (which will not be used in any calculation). For each group, display the number of characters on the line, the smallest digit on the line, the lowercase letter closest to the beginning of the alphabet, and the uppercase letter closest to the end of the alphabet. Output has to display column headings as shown below.

Sample input file:
3
2 f 6 8 D 9 0 B Gw c k @
A B C D E F G z w v e 9 7 2 @
8 7 6 3 w e r t y u I o p l k j h g A X D T Y U p 7 1 @

Sample output

Line   # of Chars   Smallest digit   Least little letter    Biggest big letter
1           12                  0                       c                            G
2           14                  2                       e                            G
3           27                  1                       e                            Y

I know nothing about C++ and she is lost, any pointers on how to write this would be a huge help!

Offline RBeen

  • (SOL) Navy Members
  • dedicated
  • ***
  • Posts: 964
  • Kudos: +34/-91
  • Canada Goose
    • View Profile
Re: C++ Help
« Reply #1 on: October 30, 2016, 10:00:40 am »
Since this program has to run until the end of a file, the outermost loop in main() is going to be something like
int main()
{
      ifstream file;
      //open file here
      //print the header for each column here
      while (!file.eof)      //if eof has not yet been reached, there is more to do
      {
              //read each character from the file, see what kind of character it is, then deal with them independently)
      }
}

Since leadtail said that you've handled opening the file, I'll skip that part.
There's a few ways to go about actually solving the problem for reading the individual lines. Probably the easiest is to take in an individual character from file, then see what kind of character it is using the different functions in <ctype.h> (make sure to cast the character to an integer when you pass it to functions like isdigit).
http://www.cplusplus.com/reference/cctype/

The first check you should do is to see if it is an '@', because then you should print out that line's statistics, and use continue to carry on to the next line.

Also, keep in mind that characters are just stored as integers, and that 'a' is guaranteed to be 1 less than 'b' which is guaranteed to be 1 less than 'c', all the way up to 'z'. The same goes for the uppercase letters (although 'z' and 'A' are separated by more than 1 value). This allows you to use the less than and greater than comparisons on the characters to sort out which is earlier or later.

Hopefully that helps!

EDIT:
In order to read a character from a line, just have a character variable (let's say it's char c) that has been declared out of the loop.
Just use file >> c and that will read the next character from the file.
Then you can check to see what sort of character c is. If you haven't been taught ctype.h and are hesitant to use it, the other way would be to do the following comparisons:

if ('0' <= c && c <= '9') // this will check to see if it is a digit
if ('a' <= c && c <= 'z') // this will check to see if it is a lowercase character
if ('A' <= c && c <= 'Z') // uppercase character

NOTE: it is very important to follow the capitalization and single quotation marks above.

for each situation, then see if it is greater or lesser than the current extreme value you have stored:
let's say you have the variable char lowestDigitThisLineSoFar.
After you have verified that c is a digit (the first if statement above)
if (c < lowestDigitThisLineSoFar) lowestDigitThisLineSoFar = c;

The very first if statement you should have is the if (c == '@') though since you don't want to update anything if that is the character being read.
« Last Edit: October 30, 2016, 10:29:00 am by RBeen »

Offline leadtail

  • (SOL) Navy Members
  • not so noob
  • ***
  • Posts: 131
  • Kudos: +9/-7
  • noob
    • View Profile
Re: C++ Help
« Reply #2 on: October 30, 2016, 10:23:24 am »
Here is the start of her code.

int main()
{
   ifstream infile;
   infile.open("letters.dat");
   ofstream outfile;
   outfile.open("Fisher_Emily_3A.txt");


   int num, numSets, sum, line, i;
   double smallest;
   char biggest, least, letter;
   sum = 0;
   line = 0;
   i = 0;

   cout << "Line" << setw(7) << "# of Chars" << setw(3) << "Smallest Digit";
   cout << setw(3) << "Least Little Letter" << setw(3) << "Biggest Big Letter\n";

   if (infile)
   {
      while (!infile.eof)
      {
         //?
      }
   }
   else
      cout << "Problem opening the input file.\n";

   infile.close();
   outfile.close();

   system("pause");
   return 0;
}

after the while loop whats the code to read the first char? After that we will find out what the char type is then do if else if to tell it what to do with it correct?

Offline leadtail

  • (SOL) Navy Members
  • not so noob
  • ***
  • Posts: 131
  • Kudos: +9/-7
  • noob
    • View Profile
Re: C++ Help
« Reply #3 on: October 30, 2016, 10:58:37 am »
int main()
{
   ifstream infile;
   infile.open("letters.dat");
   ofstream outfile;
   outfile.open("Fisher_Emily_3A.txt");


   int num, numSets, sum, line;
   double smallest;
   char biggest, least;
   sum = 0;
   line = 0;

   cout << "Line" << setw(7) << "# of Chars" << setw(3) << "Smallest Digit";
   cout << setw(3) << "Least Little Letter" << setw(3) << "Biggest Big Letter\n";

   if (infile)
   {
      while (!infile.eof)
      {
         line++;
         infile >> num;
         if (num == '@')
         {
            if (isdigit(num))
            {
               if (num < smallest)
                  smallest = num;
            }
            else if (isalpha(num))
            {
               if (isupper(num))
               {
                  if (num > biggest)
                     biggest = num;
               }
               else
               {
                  if (num < least)
                     least = num;
               }
            }
         }
         else
         {
            cout << line << setw(7) << sum << setw(7) << smallest << setw(7) << least << setw(7) << biggest << endl;
         }
      }
   }
   else
      cout << "Problem opening the input file.\n";

   infile.close();
   outfile.close();

   system("pause");
   return 0;

Here is where we are now but it is giving her a fail and not running.

Offline RBeen

  • (SOL) Navy Members
  • dedicated
  • ***
  • Posts: 964
  • Kudos: +34/-91
  • Canada Goose
    • View Profile
Re: C++ Help
« Reply #4 on: October 30, 2016, 11:16:34 am »
Right now you're taking your input as an integer - this will mean that most of your file will get discarded, since the ifstream is looking for an integer. You need to take in a character type so that you will get any input, and then you can decide what it is.
Also, with how your ifs are structured, you should have if(num != '@') instead.
Also, be sure that the bracket for main is ended in the correct spot, and that you have #include <ctype.h>

Offline leadtail

  • (SOL) Navy Members
  • not so noob
  • ***
  • Posts: 131
  • Kudos: +9/-7
  • noob
    • View Profile
Re: C++ Help
« Reply #5 on: October 30, 2016, 12:46:47 pm »
/*
Emily Fisher
CMPS 1044 - Wutrich
Program 3A
11-1-16
*/

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cctype>

using namespace std;

int main()
{
   ifstream infile;
   infile.open("letters.dat");
   ofstream outfile;
   outfile.open("Fisher_Emily_3A.txt");


   int numSets, sum, line;
   double smallest;
   char num, biggest, least;
   sum = 0;
   line = 0;

   cout << "Line" << setw(7) << "# of Chars" << setw(3) << "Smallest Digit";
   cout << setw(3) << "Least Little Letter" << setw(3) << "Biggest Big Letter\n";

   if (infile)
   {
      while (infile >> numSets)
      {
         line++;
         infile >> num;
         if (num != '@')
         {
            if (isdigit(num)) // this will check to see if it is a digit
            {
               if (num < smallest)
                  smallest = num;
            }
            else if (isalpha(num))
            {
               if (isupper(num))
               {
                  if (num > biggest)
                     biggest = num;
               }
               else
               {
                  if (num < least)
                     least = num;
               }
            }
         }
         else
         {
            cout << line << setw(7) << sum << setw(17) << smallest << setw(17) << least << setw(17) << biggest << endl;
         }
      }
   }
   else
      cout << "Problem opening the input file.\n";

   infile.close();
   outfile.close();

   system("pause");
   return 0;
}

Here is where we are now, but we are getting a can not find PDB file and is failing. Is this an program install problem?

Offline RBeen

  • (SOL) Navy Members
  • dedicated
  • ***
  • Posts: 964
  • Kudos: +34/-91
  • Canada Goose
    • View Profile
Re: C++ Help
« Reply #6 on: October 30, 2016, 01:19:15 pm »
I'm not sure why the while loop got changed, but it should still be !infile.eof.
That doesn't sound like a normal C++ code error. Google it for your IDE and see what comes up.

Offline leadtail

  • (SOL) Navy Members
  • not so noob
  • ***
  • Posts: 131
  • Kudos: +9/-7
  • noob
    • View Profile
Re: C++ Help
« Reply #7 on: October 30, 2016, 02:27:15 pm »
Well we changed the !infile.eof because we where getting an illegal operator error, and the other way is the way the teacher told her was better. I really do appreciate the help. She is headed back to school now and is going to have the teacher look at what she has done and help her finish it. If it is okay I may ask for your help on the next one, as she understood your explanations of what to do. Gig thumbs up man!

Offline RBeen

  • (SOL) Navy Members
  • dedicated
  • ***
  • Posts: 964
  • Kudos: +34/-91
  • Canada Goose
    • View Profile
Re: C++ Help
« Reply #8 on: October 30, 2016, 03:25:06 pm »
If you're using the while (!infile >>) it should be into num, and then you should use that value. Right now it's throwing away half of the input file.
No problem, hopefully the teacher can get it sorted out.