Jump to content

  • Log In with Google      Sign In   
  • Create Account


Youbar

Member Since 12 Oct 2012
Offline Last Active Dec 07 2012 10:36 PM

Topics I've Started

(C++) Printing Out 2d Array

06 December 2012 - 06:15 PM


int map[5][5] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 2, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};

int column = 0;
int column2 = 0;

()void print_map()
void print_map()
{
for (column = 0; column < 6; column++)
{
cout << map[column][column2];
if (column == 5)
{
  column2++;
  column = 0;
  cout << endl;
}
if (column && column2 == 5)
{
  break;
}
}
}
This is my code for printing out a 2d array.
What happens in the output though, is unexpected. It produces:

111110
00010
02010
00010
11110
1

Instead of:
11111
10001
10201
10001
11111

1. How can I fix this problem?
2. Can you call the an if command an exception?
3. Please don't post the fixed code. Just give me hints, unless the code is beyond fixing.

Confused by Pointers (Beginner's Script):

04 December 2012 - 07:06 PM

// more pointers
#include <iostream>
using namespace std;
int main ()
{
  int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
  p1 = &firstvalue;  // p1 = address of firstvalue
  p2 = &secondvalue; // p2 = address of secondvalue
  *p1 = 10;		  // value pointed by p1 = 10
  *p2 = *p1;		 // value pointed by p2 = value pointed by p1
  p1 = p2;		   // p1 = p2 (value of pointer is copied)
  *p1 = 20;		  // value pointed by p1 = 20
  
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}

I understand all but this line of the script, which I don't see any reason for:
p1 = p2;		   // p1 = p2 (value of pointer is copied)

I can see that it says it was copied, but I thought that already happened in the line above that?
*p2 = *p1;		 // value pointed by p2 = value pointed by p1

Variables within strings? (C++)

01 December 2012 - 03:36 AM

I'm just going to get straight to the point:
Using "cout", I can get text, then a variable, then some more text. I wondered if I could replicate that into a string?
e.g
cout << "Hi, I am " << x << "years old."; into a string.

EDIT: I've tried something with a string, but I've gotten a compile error:
invalid operands of types `const char[22]' and `int' to binary `operator<<'

The string is as follows:
string day1_c_1 = "You, along with your " << rs_q_c << " friends, set up camp outside of town. You carried " << rs_q_f << " units of food with you, along with another " << rs_q_m << " units of metal.";

Program that prints out array is not working

15 October 2012 - 09:50 PM

#include <iostream>
using namespace std;
int map[5][5] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
//other stuff
int mapn = 0;
int mapn2 = 0;
int number = 0;
int number2 = 0;
int x, y = map[x][y];
int main()
{
    x = map[2][y];
    y = map[x][2];
    while (mapn < 5, mapn2 < 5)
    {
	   cout << map[number][number2];
	   mapn++;
	   number++;
    if (mapn == 5)
    {
	   mapn2++;
	   number2++;
	   mapn = 0;
	   cout << endl;
    }
    }
cout << endl;
system("PAUSE");
}

This program is supposed to print out an array. To do so, it prints out a number, adds onto that number and once that number equals five, number2 has 1 added onto it, and number is reset. Why isn't it working?
It compiles fine, but the output is a lot of random numbers. :(

C++ % Operator Problem

12 October 2012 - 10:53 PM

Hi, I'm just starting out C++ and decided to have an attempt at making a calculator, probably very crude but it works. Anyway, I'm having a problem with the % operator. This is only a portion of my code, but you'll get the gist:

else if(response == 5) {
	   cout << "Remainder" << endl;
	   cout << "Enter first number: ";
	   cin >> fn;
	   cout << "Enter second number to divide and get remainder: ";
	   cin >> sn;
	   float ovrl = (fn % sn);
	   cout << ovrl << endl;
	   system("PAUSE");
	   goto startagain;
    }

The code returns this error:
invalid operands of types `float' and `float' to binary `operator%'

Full code here:
Spoiler

PARTNERS