nested for statements

This is a discussion on nested for statements within the C++ Programming forums, part of the General Programming Boards category; I do not understand what happens when you have more than one for statements in the same block! I am ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    14

    nested for statements

    I do not understand what happens when you have more than one for statements in the same block! I am messing around trying to make a program that reverses whatever you type in (I did this type of thing in class, last year for VB.net), but so far it is not working. I just reworked this before I posted it, I think it may work now:

    Code:
    #include<iostream>
    #include <cstdlib>
    using namespace std;
    int main() {
    
    char tar [10];
    char rat [10];
    int length, i, b;
    length = strlen(tar);
    cout << "Enter a string: ";
    cin.get(tar);
    for(i = 0; i < (length -1); i++){ 
    	for(b = length; b >= 0; b--) 
    		rat[i] = tar[b];
    }
    cout << "Your string reversed is: " << rat << i << ' ' <<b;
    return 0;
    }
    And the error I get:

    .\hypotenuse.cpp(11) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem &)' : cannot convert parameter 1 from 'char [10]' to 'char &'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]

    Don't mind the strange file name, I just reuse the same file for screwing around, to prevent a massive directory of useless programs. BTW I give my variables and that strange names because I'm to lazy to think up a descriptive name, nor do I care, after all, it's not like anyone else is going to use this (I'm not making an application or anything :P)

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Please search the forum for "reverse a string," you will find many answers there.
    > Reversing Strings

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The for statements should compile ok. The compile error you get is because you are calling get and passing in a character array. The get() function takes a single character. Perhaps you meant to use getline.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Better yet, use proper C++ strings with the free getline(). The way it is you're in grave danger of buffer overflows.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    By the way, what did you expect
    Code:
    char tar[10];
    int length = strlen(tar);
    to do? The array tar isn't initialized to anything. Unless, of course, you wanted to get it's length AFTER you had input something into it.



    As previously mentioned, your input code is wrong.


    You should use only one for loop, with both variables initialized in it.


    You can use std::string instead of char[], which is messy.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you insist on using char arrays try this way
    Code:
    #include<iostream>
    //#include <cstdlib>   // not needed here
    #include <cstring>     // for strlen()
    
    using namespace std;
    
    int main() {
    
        char tar [10];
        char rat [10];
        int length, i;
        cout << "Enter a string: ";
        cin.get(tar, sizeof(tar) );   // this version of get needs the size of the buffer
        length = strlen(tar);         // must be called after input
        
        for(i = length-1; i >= 0; i--) // one loop should do
            rat[length-i-1] = tar[i];
        
        rat[length]='\0';              // needs a terminating zero
        
        cout << "Your string reversed is: " << rat << endl;
        return 0;
    }
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 09:29 PM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 09:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 10:57 AM

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