Barchart and unfamiliar compiling errors

This is a discussion on Barchart and unfamiliar compiling errors within the C++ Programming forums, part of the General Programming Boards category; Hello, I've attempted to create program that will create a bar chart of stars to look like this... -------------------------------------- 1: ...

  1. #1
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33

    Barchart and unfamiliar compiling errors

    Hello, I've attempted to create program that will create a bar chart of stars to look like this...

    --------------------------------------
    1: ***
    2: *

    and so on, it has to read the amount of 6 numbers from the command line and then print the correct amount of stars for each, each time I try and compile this program i get the errors

    1. ANSI C++ forbids comparison between pointer and integer

    2. In Function 'int main ( int, char**)

    I am very very new to c++ and these are errors I have never come across before, and frankly the program I have been asked to write is way out of my depth.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <assert.h>
    using namespace std;
    
    
    void main ( int n, char c)
      /*int main (int argc, char* argv [])*/
    {
      int n [6];
    
      char c;
      int total = 0;
    
      
       if ( argc != 7 )
       {
          cerr << "Usage: ./a.out n1 n2 n3 n4 n5 n6\n" ;
          exit(1) ;
       }
    
    while (  n  < 6)
        {
          cin >> n [c];
          c++;
        }
     for ( int i = 0; i < 6; i++)
       {
         n [i] = atoi ( argv [i+1]);
         assert (0 <= n [i] && n [i] <= 100);
       }
    
         for ( int i = 0; i < 6; i++)
           {
    	 total += n [i];
           }
         cout <<"--------------------------------------------------/n"
              << total * c << "*";
    }
    This is what I have written, whether it even does what i want it to i do not know, but I gave my best shot.

    any suggestions for why it keeps throwing up errors would be greatly appreciated.

    thanks Karla.
    Microsoft: "You have questions, we having dancing paperclips"

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your declaration of n
    Code:
    int n [6];
    the error
    Code:
    while (  n  < 6)
    n is an array of 6 ints.
    in c the name of an array becomes a pointer to its first element
    so here you compare an integer ( 6 ) with a pointer to an int ( n ).

    some more error
    Code:
    cin >> n [c];
    Don't know what you try to do but c is declared to be a char ( not very common to use it as index into an array, but possible ). Trouble is it's not initialized.


    Kurt

  3. #3
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    oh okay, I'll have a go and see if I can get it to work now.

    Thanks for replying :-)
    Microsoft: "You have questions, we having dancing paperclips"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    void main ( int n, char c)
    /*int main (int argc, char* argv [])*/

    So why did you comment out the good one and replace it with a broken one?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    London
    Posts
    14
    Code:
    while (  n  < 6)
        {
          cin >> n [c];
          c++;
        }
    You dont need it. You are supposed to input data from command line.

Popular pages Recent additions subscribe to a feed

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