Saith 74 Junior Poster

Glad you got the answer :D

I would suggest you use the following whenever you use a file for data. It may help with an immediate solution that would otherwise be directly hidden from you.

ifstream fin;
	fin.open ("1.txt");
	if(fin.fail() ){
		cout << "Input file failed to open.\n";
		exit(1);
	}

You will be notified if the file is truly there, or not. Should help resolve any potential problems from reading in values to an array from a nonexistent file, or a file not found.

Saith 74 Junior Poster

You also may want to read up on strings and string input, from what I read from multiple other sources, you want to be using the standalone function getline(cin, npcname) vs the regular cin >> input. Mixing cin >> with strings may give some funky results.

Saith 74 Junior Poster

This is what I ended up coming up with. First you want to make sure you can open your file. If you cannot open your file, your arrays will automatically be filled with junk. That may be half of your problem.

After that, I just used cout << as a basic step by step debugger to make sure my input into the array was truly valid.

I also changed this from a void() to a main() as I didn't have anything else to go by. Good luck to you and hope this answers your question.

#include <iostream>
#include<fstream>
using namespace std;

int main() {

	int ArrayA[10], ArrayB[20], ArrayN[20];

	ifstream fin;
	fin.open ("1.txt");
	if(fin.fail() ){
		cout << "Input file failed to open.\n";
		exit(1);
	}

	for(int index = 0; index < 10; index++)
		fin >> ArrayA[index];

	for(int index = 0; index < 10; index++)
		cout << "The Array A["<< index << "] " <<ArrayA[index] << endl;


	fin.close();

	ifstream fin2;
	fin2.open("2.txt");
	if(fin2.fail() ){
		cout << "Input file failed to open.\n";
		exit(1);
	}

	for(int index=0; index<20; index++)
		fin2 >> ArrayB[index];

	for(int index = 0; index < 20; index++)
		cout << "The Array B["<< index << "] " <<ArrayB[index] << endl;

	fin2.close();

	//N=A+B to the first 10 in the array	
	for(int index=0; index < 10; index++)
		ArrayN[index] = ArrayA[index] + ArrayB[index]	;	

	// For N[10 - 19] = B[10-19] in the array as ArrayA only goes up to 9, not 19.
	for(int index = 10; index < 20; index++)
		ArrayN[index]  = ArrayB[index];
Saith 74 Junior Poster

This is what I came up with. The code works for any valid input from 1 - 9. You can change the range from a number higher than 9 or lower than 1.

#include<iostream>
using namespace std;


int main(){

	int number;

	cout << "Please input a number 1 - 9.\n";
	cin >> number;

	if(number < 1 || number > 9){
		cout << "Input valid number between 1 - 9.\n";
		exit(1);
	}

	cout << "You input was " << number << endl;

	return 0;
}
Saith 74 Junior Poster

AH. I got it!

int problem[9];

/* To get 9 object into the array, you need to declare as you did

int problem[9];

but you failed to realize, or at least looked over the small detail that the array starts at [0], and not [1], the array goes from problem[0] to problem[8] NOT problem[1] to problem[9]. 

If you change problem to problem[10] as such,

int problem[10];

Then you can skip the first entry [0], and continue as you did before. As such, the debugging error that I had received before is now gone. Copy and past what I have written, just change your array up top to,

int problem[10];
Saith 74 Junior Poster

This is what I ended up writing.

#include <cstdlib>
#include <iostream>

using namespace std;

void compare(int problem[], int SIZE);

int main(){

.
.
.
.

/* I placed this following section at the very end of your program, between the last else statement and your system pause. */

    int SIZE = 10;   // you set up your array wrong, array start at 0, not 1.      
		// problem[0] is the first location in memory, not problem[1].         
                    // Suppose it was clarity from science to programming.

    compare(problem, SIZE);
    
    // System Pause
   system("PAUSE");
   return EXIT_SUCCESS;

}

void compare(int problem[], int SIZE) {
	int max = problem[1];
	for(int index = 1; index < SIZE; index++){
		if(max < problem[index])
			max = problem[index];
	}
	cout << "Maximum value from 1 - 9 in the array: " << max << endl;

	if(max == 1)
		cout << "Type string name 1 here" << endl;
	else if (max == 2)
		cout << "Type string name 2 here" << endl;
	else if (max == 3)
		cout << "Type string name 3 here" << endl;
	else if (max == 4)
		cout << "Type string name 4 here" << endl;
	else if (max == 5)
		cout << "Type string name 5 here" << endl;
	else if (max == 6)
		cout << "Type string name 6 here" << endl;
	else if (max == 7)
		cout << "Type string name 7 here" << endl;
	else if (max == 8)
		cout << "Type string name 8 here" << endl;
	else if (max == 9)
Saith 74 Junior Poster

I just wrote it. You can copy and paste what I wrote and build it. It should work for you. You may want to toss your while loop back in there to check to make sure that the input is valid, between 1 and array size-1.

// This part, as copied and pasted from my previous note, will output the desired array to cout.

for(int index = 0; index < number; index++){
cout << name[number-1][index];
}
Saith 74 Junior Poster

Seem like your coding structure is a little off, perhaps something like this?

This is just a snippet of what I just wrote, but I did write the entire program to make sure the snippets work properly. The output seems correct with the desired input in the array. I won't copy and paste the entire program of what I have written just to do someone elses homework, but snipped what I did write to help that person understand how I came up with that I did.

I recommend using comments as I have done here in your own program, just in case someone else needs to read what you were trying to do. Also, allows you to remember what you did at a later date.

/* This truly brought me back to the days when I first learned how to do functions. Let's start
from the beginning.

What I'm about to write is all in assuming from what I gethered from the given code.

Find:

Average of all Positive Numbers
Average of all Negative Numbers
Average of all Numbers;
and Max Number in the list.

with a given array. Make function use out of all required findings. */


#include <iostream>
using namespace std; 


// declarations purposely deleted from this snippet

int main()
{

int Ave, AveP, AveN, Max;		// integers record required averages
const int ARRAY_SIZE = 10;

int numbers[ARRAY_SIZE]={4, -30, 0, 7, 42, -20, 18, 400, -123, -6};	// array that will be manhandled

/* Display(numbers, …
jonsca commented: It's good that you did not give a complete solution. +6
Saith 74 Junior Poster

As Fbody mentioned, change the size of your array accordingly. To output any type of array, especially two dimensional, use for loops.

#include <iostream>
using namespace std;

int main () {


	char name[20][20] = {"1. One",
"2.Two",
"3.Three",
"4. Four",
"5. Five",
"6. Six",
"7. Seven",
"8. Eight",
"9. Nine",
"10. Ten",
"11. Eleven",
"12. Twelve",
"13. Thirteen",
"14. Fourteen",
"15. Fifteen",
"16. Sixteen",
"17. Seventeen ",
"18. Eighteen ",
"19. Nineteen",
"20. Twenty"

};

	int number;

	cout << "Enter the number, I will repeat after you.\n";
	cin >> number;

	for(int index = 0; index < number; index++){
		cout << name[number-1][index];
	}

	return 0;

}
Saith 74 Junior Poster

The question is why you would want to know the exact location of variable char a. It's useless as when the variable is initialized with the onset of the program, the variable will be in a different memory location. Hence, hard coding memory locations will be meaningless.

#include<iostream>
      using namespace std;
 
  int main(){


char a='p';
char *point;

point = &a;        // point =  memory location of char a

cout << a << " - " << &a << endl;
cout << *point << " - " << point << endl;

/* This will only move up or down one memory location at a time, not necessarily moving up or 
down one character variable location. */

cout << "Moving down one memory location.\n";
point --;
cout << *point << " - " << point << endl;
cout << "Moving up one memory location.\n";
point++;
cout << *point << " - " << point << endl;

/* To move up and down an array, such as an array of characters (char), you will want to reference
your pointer as such*/

char b[10];
int index;

for(index = 0; index < 10; index++)		// change array b to have some comprehensible character
	b[index] = 'a' + index;

point = b;					// point points to location b[0];
cout << point[1] << endl;			// output location 2 (TWO) in the array
cout << b[1] << endl;				// point[1] should be equal to b[1];

point = b;

			
return 0;


      }
Saith 74 Junior Poster

Can you paste the code that you have written so far?

Saith 74 Junior Poster

Will do in the future, Alexchen. I'm an absolute noob to forums, and just started today at this site. Thank you for the advice!

Saith 74 Junior Poster

::Open mouth, insert foot::

You're right. I was *assuming* that if you could create a char by my previous statement than character with [1] array would be the same, only a character of 1 input. Even if you wanted to, needs to be a minimum of [2] for the NULL as described above. Else you get a debug error.

You are correct, Jonsca. In the future, I'll put it in a more questionable format for others to confirm instead of a 'matter of fact' phrase, and thoughtless ponder to actual trials.

Saith 74 Junior Poster

Instead of having the user input a statement, have the program ask a question and the user reply with yes or no. At which point, if the answer is yes, you lead down one road of possibilities to what the answer is. Otherwise, head the other direction. Having the user to input an answer for the program to comprehend might be a bit much for a simple '20 Questions'.

e.g. Answer: Pencil

Q1. Person(A), Place(B) or Thing(C).
Input: C
Q2. Solid Object?(A) Non-solid Object (B)
Input: A
Q3. Human daily use?(A) Or not daily use?(B)
Input: A
Q4. Used for communication?(A) Or not used for communication(B)
Input: A
Q5. Do you write on it?(A) Or do you not write on it. (B)
Input: B
Q6. Do you write with it?(A) Or do you not write with it? (B)
Input: A
Is the answer: A pencil?

That's very simplistic and very narrowed approach to the answer but -more of the set up that I was thinking-. This would be one approach, definitely may not be the best approach, but one that could possibly do the job. Good luck!

Saith 74 Junior Poster

#include <string>

You can assign value by,

int main() {

string name;
name = "This is a string. Don't forget to use double quotes instead of single!";

cout << name;

Output: This is a string. Don't forget to use double quotes instead of single!


This string class has a lot of member functions that you can use. I'm not sure if we are allowed to link websites on the forums, but you can check out:

http://ruby-doc.org/core/classes/String.html

for a list of full detailed member functions for the String class.

Saith 74 Junior Poster

I feel like a goof, trying to help out but upload incorrect programming on my very first entry! ><

CORRECTION:
function(happy_num); // pass by value
function2(happy_num); // pass by reference

Incorrect:
unhappy_num = function(happy_num); // pass by value
unhappy_num = function2(happy_num); // pass by reference

** voids can not return int **

Saith 74 Junior Poster

Just a quick note. What you had earlier,

char name;

is the same as

char name[1]; // variable holding 1 character

// They changed it to 30,

char name[30];

// so that any input you enter will have enough space to be saved to the variable "name".

Saith 74 Junior Poster

I just started back into C++ from taking some time off from it as well, slight rusty, but let's see if I can give you a shot at what you are asking for.

class FullOfFunctions{
public:

void function(int happy_num); //pass by value
void function2(int & happy_num); //pass by reference
private:
};

When you use the function for either in the main() program, their use will look the same.

int main() {

int happy_num, unhappy_num;

unhappy_num = function(happy_num); // pass by value
unhappy_num = function2(happy_num); // pass by reference

return 0;
}

void FullOfFunctions::function(int happy_num) {
//purposely left blank
// random code
}
void FullOfFunctions::function2(int & happy_num){
// purposely left blank
// random code
}

As far as I'm aware, even with input/output streams to class member functions, you will not need the & while calling the function in the main() program. Don't quote me on this but the compiler will know prior to manifesting the program if the variable passed, including passing objects of classes or structures, will be either "pass by variable or reference."

I hope this helps.