array math

This is a discussion on array math within the C++ Programming forums, part of the General Programming Boards category; Hey, Here is my code: Code: #include <iostream> using namespace std; int main(void){ int x, i, total, perc; char n[25], ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    38

    array math

    Hey,

    Here is my code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void){
    
    	int x, i, total, perc;
    	char n[25], ar[20][20];
    
    
    	cin >> x;
    	cin.ignore();
    
    	for(i = 0; i < x; i++){
    	
    		cin.getline(n, 25);
    		cin.ignore();
    
    		cin.getline(ar[i], 20);
    		cin.ignore();
    		
    		
    		perc = ((ar[0][0] * 100) / ar[0][1]);
    
    		cout << n << " " << perc << "%";
    		cin.ignore();
    	
    	
    	}
    	
    
    return 0;
    }
    '

    Code:
    Input:
    
    1 (number of inputs)
    Name (name of song)
    48 52 (amount earned out of total)
    
    Find the percentage
    Well it doesn't work. I don't know whats going on with the arrays. ar[0][0] isn't 48 like i expected it to be.. same with ar[0][1]...

    instead ar[0][0] somehow = 56 and ar[0][1] = 8... confused.

    help please

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, the character that is output as '0' has an ascii value of 48 (and so on for other digits).

    Probably you meant to use an array of numeric values for calculations.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    How would I make them numeric values... an array can't really be an "int" right?

    So I tried:

    int val2, val2;
    val1 = ar[0][0];
    val2 =ar[0][1];

    but when i display their contents after entering 48 52 i get 56 32...
    Last edited by me77; 03-09-2010 at 10:35 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int array[10]; //ten integers
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by me77
    How would I make them numeric values... an array can't really be an "int" right?
    An array cannot be a char either
    But what you should do is subtract '0' (as in the character), i.e., you will end up subtracting 48 from 48 to get 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21