Yo I need help!!!

In my class we had to be able to store chars, ints, and strings inside of both a string and an array. Then switch around or shuffle the positions of the data stored. I was able to write programs to do it for both the int and char data type but I'm completely lost on how to do it for the string data type. Here's what I what I had what I had for the char data type. How would I do the same thing but for strings??

#include<iostream>
#include<string>

using namespace std;

int main() {
char currentchar;
string storage1;
char storage2[5];
char store;
char store2;


cout << "Please enter 5 characters to store in a string and array" << endl;
for (int i=0; i < 5; i++) {
cin >> currentchar;
storage1 = storage1 + currentchar;
storage2[i] = currentchar;
}
store = storage1[0];
storage1[0] = storage1[4];
storage1[4] = store;

store2 = storage2[0];
storage2[0] = storage2[4];
storage2[4] = store2;


cout << storage2 << endl;
cout << endl;
cout << storage1 << endl;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
  std::string Array_of_strings[] = { "this", "is", "an", "example" };
  std::cout << Array_of_strings[2] << std::endl;
  
  return 0;
}
I understand how you are like assigning each word to a spot in the array but how would you do this with a line a data a user entered???
Last edited on
closed account (48T7M4Gy)
You mean, like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{  
  std::string Here_we_go_again[2] = {""};
  
  std::cin >> Here_we_go_again[0];
  std::cin >> Here_we_go_again[1];
  
  std::cout << Here_we_go_again[0] << ' ' <<  Here_we_go_again[1] << std::endl; 
  
  return 0;
}
Last edited on
Lol the name of that array was pretty funny... but thank you for your help I appreciate it. The program is doing what i want for the most part and I can switch around words in the array but how would you switch around the words in the string.




These were my guidelines for the assignment

1. Begin a simple program that will ask the user for some different types of input. We’ll be
storing this input in both a string and an array. Remember to include the string header file
to get all of the string capabilities.
2. We’ll start by asking for char input. Declare a string and an array to store the input. One of
the major differences between strings and arrays is that you can always append information
onto a string using concatenation or the + operator, BUT arrays have fixed storage space.
So you’ll have to initialize a size for the array and let the user know there’s a limit to the
number of characters they can store.
3. Iteratively ask the user to enter data, which will then be stored both in your string and your
array. One straightforward way to go about this is storing the input into a char variable and
then appending the information to the string, and initializing the appropriate storage space
in the array using the [ ] operator.
4. Attempt to shuffle the information stored. IE, try to switch the information stored in the
first position of the string and array with the information stored in the third position and
vice versa. Is switching one storage method more straightforward and if so why?
5. Repeat the above steps twice, first using integers instead of chars and then using strings.
closed account (48T7M4Gy)
The guidelines appear to be written by someone who doesn't expect to be understood, so I can only guess. :(

It seems the exercise revolves around strings vs c-style strings rather than what I addressed above.

If I am correct then you need to checkout the functionality of each data type. Each has a large number of functions you can draw on to manipulate each:

a) c-string ( an array of characters ) http://www.cplusplus.com/reference/cstring/
b) string class http://www.cplusplus.com/reference/string/

It's worth spending a bit of time surfing through these functions. There are plenty of demo examples.



Aight thanks for helping me out man I appreciate they help
closed account (48T7M4Gy)
Stay tuned though I'm just having a closer look. I'll be back soon
Unfortunately with no great revelations. :)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<string>

using namespace std;

int main()
{
	  char currentchar;
	string storage1;
	  char storage2[6]; // Extra char to allow terminal character
	
	// PART 1
	cout << "Please enter 5 characters to store in a string and array" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << i + 1 << ": ";
		cin >> currentchar;
		storage1 = storage1 + currentchar;
		storage2[i] = currentchar;
	}
	storage2[5] = '\0'; // c-string terminal character
	cout << storage1 << ' ' << storage2 << endl;

	//PART 2 - first with third
	char temp = storage2[0];
	storage2[0] = storage2[2];
	storage2[2] = temp;

	temp = storage1[0];
	storage1[0] = storage1[2];
	storage1[2] = temp;

	cout << storage1 << ' ' << storage2 << endl;

	// PART 3
	int numbers[5] = { 0 };
	
	cout << "Please enter 5 numbers to store in an array" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << i + 1 << ": ";
		cin >> numbers[i];
	}
	// List out numbers
	for (int i = 0; i < 5; i++)
		cout << i + 1 << ": " << numbers[i] << endl;

	// Swap numbers
	int temporary = numbers[0];
	numbers[0] = numbers[2];
	numbers[2] = temporary;
	
	// List out modified numbers
	for (int i = 0; i < 5; i++)
		cout << i + 1 << ": " << numbers[i] << endl;


	system("pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.