HELP WITH STRING, C-STRING, VECTOR

closed account (iE6792yv)
error
Last edited on
Hello ggucciflipflopps666,

In your subject do not SHOUT. This does not make any replies any quicker.

So, What is the problem or do you expect someone to figure it out for you?

Unless you are writing to your files in binary mode the "DAT" extension should be "txt".

So far I have not seen any use for the header file "<cstring>". If you do not need it it is just extra code that makes the program bigger for no reason.

Andy
closed account (iE6792yv)
error
Last edited on
Hello ggucciflipflopps666,

These days I understand the easier to read part. If you do not know about it, hold down the "Ctrl" key while rotating the mouse wheel. In most programs this will increase or decrease what is displayed on the screen.

Do not update and change your original post when it come to code. Changing the whole program like you have is confusing to people. post your changes in a new post especially if you have major changes to the program.

To start with you have changed the code, and I have the idea this is to use C-strings, but some of the code still is trying to use "std::string"s and "std::vector"s which is giving errors at compile time because the header files are missing. For now the simple answer is to put the header files back until you can remove the code using the "std::string"s and "vector"s.

The better option is to remove the code using "std::string"s and "std::vector"s and just deal with the C-strings.

I will work on it more after I get something to eat.

Hope that helps,

Andy
Hello ggucciflipflopps666,

This is what I have managed to work up so far.

For the header files the order should not make any difference, but when I do this:
1
2
3
4
5
6
#include <cstring>
#include <iostream>
#include <iomanip>
#include <limits>

#include <fstream> 

I find the alphabetical order helps to remind you if you miss something. Since "iomanip" works with "iostream" I just follow "iostream" with "iomanip" even though its not the proper order. After the blank line I put any header files that are needed by the program. I find this helps.

For the line: const string OUTPUTFILE_NAME = "OUTPUT.TXT"; I did change "DAT" to "TXT" as "dat" tends to refer to a different type of file.

For the void "Enter_A_Sentence" function I changed it to: void Enter_A_Sentence(char inputStr[]); in order to use C-strings. Which makes the function call: Enter_A_Sentence(inputStr);.

After the menu my adjustments are 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
29
30
31
32
33
34
35
36
	int option;
	cin >> option;

	while (!cin) // <--- catches anything that is not a number.
	{
		std::cout << "\n    Invalid entry try again.";

		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

		cout << "\n\t Option: ";
		cin >> option;
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	switch (option)
	{
	case 1:
		Enter_A_Sentence(inputStr);
		std::cout << "\n " << inputStr << std::endl;
		//Parse_InputString_Into_ArrayStructure(inputStr, Cstrings, numberOfWords); // <--- Still working on it.
		break;
	case 2:
		break;
	case 3:
		break;
	case 4:
		break;
	case 0:
		cont = false;
		break;
	default: cout << "\tERROR: Invalid option. Please re-enter.\n";
		break;
	}
} while (cont);

Line should be an "int" not a character array.

The while loop is to catch anything other than a number that would cause "cin" to fail.

A second while loop would be useful to catch any number that is out of range.

Line 9 clears the input buffer so you can start fresh.

Line 15 clears the input buffer before you use any type of "getline". Otherwise the formatted input of "cin >> option" would leave the "\n" in the input that a "getline" would extract without waiting leaving you unable to enter anything.

For case 0, first I defined bool cont{ true }; where "cont" is short for continue which is a key word that you can not use as a variable name.

it may have seemed easy to use "exit(0);" to leave the program, but since you are in "main" it should be "return 0;", but it is better to exit the switch and then the do/while loop then let the program end.

For the function Enter_A_Sentence I did this:
1
2
3
4
5
6
7
8
void Enter_A_Sentence(char inputStr[])
{
	cout << "\n\t\tEnter a sentence (input string) that includes spaces and ends with a period(.):\n";
	std::cin.getline(inputStr, MAX_SIZE);
	//getline(cin, inputStr, '.'); // <--- Can only use a "std::string" not a "C-string".

	cout << "\n\t\tCompleted writing input String, elements from array of C-strings,\n\t\tarray of Strings, and vector of strings into output data file\n";
}

line 4 is the replacement to enter into a C-string.

The comment on line 5 should explain it.

Line 7 does not make much sense right now, but could in the future. And personally I think you have to many "\t"s in the "cout' statements.

The function Parse_InputString_Into_ArrayStructure I am still working on, but I do know you will likely be using the function strtok() to break up the C string. I have not figured this part out. I am still getting errors dealing with "char*"s and arrays not working together along with trying to use the 2D array to store each word.

As you can see once you enter a string you will need to break it up into individual words before you can continue. At least for now this may work better to get the function running, but eventually you will want to move line 22 down to case 2.

Hope that helps,

Andy
Topic archived. No new replies allowed.