Thread: array math

  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 11: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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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