Desperate for Explanation on I/O Assignment

Hello Everyone!
I have a programming assignment I have been working on all day. I'm pretty sure I've just been overthinking it so I am going to post on here to see if I can gain a new perspective.

The assignment criteria: "Write a program that reads in characters from a file and displays the characteristics (aka: lowercase, uppercase)"

The output will be given in a file format as well. No built in character analysis is allowed.

Where I'm stuck: I can get data from a file to display but I don't know how to insert criteria functions that will take the displayed data and output the characteristics.

Any help is appreciated!!

Thanks :)


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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const std::string file_name = "whatever.txt" ;

    if( std::ifstream file{file_name} )
    {
        // since 'no built in character analysis is allowed'.
        const std::string ucase_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
        const std::string lcase_chars = "abcdefghijklmnopqrstuvwxyz" ;

        char ch ;
        while( file.get(ch) )
        {
            if( ucase_chars.find(ch) != std::string::npos ) std::cout << ch << " - upper case\n" ;
            else if( lcase_chars.find(ch) != std::string::npos ) std::cout << ch << " - lower case\n" ;
            else if( ch >= '0' && ch <= '9' ) std::cout << ch << " - decimal digit\n" ;
            else std::cout << "int " << int(ch) << " - something else\n" ;
        }
    }

    else std::cerr << "failed to open input file\n" ;
}
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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

void identify( char c )
{
   const char target[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
   cout << c << " is ";
   for ( int i = 0; i < 62; i++ )
   {
      if ( target[i] == c )
      {
         cout << ( i < 26 ? "upper case" : i < 52 ? "lower case" : "numeric" ) << '\n';
         return;
      }
   }
   cout << "non-alphanumeric\n";
}


int main()
{
// ifstream in( "test.txt" );                       // for real
   istringstream in( "C++ standardised 1998" );     // for test
   for ( char c; in >> c; ) identify( c );
}

Last edited on
I have been working on all day


So post your code so that we can see what you're done...

Without specifying an output file, consider:

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
#include <array>
#include <iostream>
#include <fstream>

std::ostream& identify(unsigned char c, std::ostream& os) {
	static constexpr auto chars { [] {
		std::array<unsigned char, 256> arr {};

		for (unsigned char i {'A'}; i <= 'Z'; ++i)
			arr[i] = 1;

		for (unsigned char i { 'a' }; i <= 'z'; ++i)
			arr[i] = 2;

		for (unsigned char i { '0' }; i <= '9'; ++i)
			arr[i] = 3;

		return arr;
	}() };

	static constexpr std::array text { "", "Uppercase", "Lowercase", "Numeric" };

	return os << c << " is " << text[chars[c]] << '\n';
}

int main() {

	if (std::ifstream ifs { "test.txt" })
		for (unsigned char c; ifs >> c; )
			identify(c, std::cout);
	else
		return (std::cout << "File not found\n"), 1;
}

Last edited on
Topic archived. No new replies allowed.