Results 1 to 5 of 5
  1. #1
    mic44's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    38
    Reputation
    10
    Thanks
    2
    My Mood
    Aggressive

    Anyone help me to make it work ?

    Hello , I am learning c++ , and im trying to do a console that simply show us : ABCD on screen in a different way than just using cout << "ABCD";

    , but I only get AAAA ! I want to make it , when count2=2 , then show B , when count2=3 , show C so I thought by incrementing my variable using count2++; might work but it doesnt ! The first time you run the loop , count2 = 1 so write A , the second time we run the loop count2 =2 so wirte B,

    P.s I want to do it with function the most simple code without tons of is/else if


    Code:
    #include <iostream>
    
    using namespace std;
    
    int incre(int a, int b, int c, int d,int count2=0)
    {
           
            switch (count2)
            {
            case 1:
                a =a;
                break;
            case 2:
                a=b;
                break;
            case 3:
                b=c;
                break;
            default:
                c=d;
    
            }
            return count2++;
    }
    
    int main()
    {
     
      int count2(0);
      
     char a('A'),b ('B'), c('C'), d('D');
        
    char generator;
        
     generator = a;
       
    for (int i=0, count2=0; i < 4; i++)
       {
          incre(a,b,c,d,count2);
          count2++;
    
           cout << generator;
       }
    
    
        return 0;
    }
    Last edited by mic44; 11-07-2014 at 01:06 AM.

  2. #2
    InunoTaishou's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    The Internet
    Posts
    446
    Reputation
    20
    Thanks
    950
    My Mood
    Relaxed
    So you're trying to print one char at a time...?

    This will print it using a function and instead of using 4 variables (char a('A'), b('B'), c('C'), d('D')) I used 1. And how to print every variable using a range for. You can do the same with a regular for, or a while.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using std::ostream; using std::string; using std::vector; using std::cout; using std::endl;
    
    //ostream &out and char &x are both parameters and they have to be &
    ostream &print(ostream &out, char &x)	//	 Create our own cout function using the type "ostream". Note: the function has to be type ostream &.
    {										//	Takes the parameters ostream& and char&
    	out << x;						//	same thing as cout << x;	Note: I did not put endl; here because ending the line is up to the call to the function
    	return out;							//	return control back to main.
    }
    
    void main()
    {
    	string str{ "ABCD" };				//	Using a string "ABCD" instead of char a('A'), b('B'), c('C'), d('D')
    	vector<char> cvec{ 'A', 'B', 'C', 'D' };		//	Same thing as the string except it's a vector with type char
    	char carr[4]{ 'A', 'B', 'C', 'D' };		//	Using a character array
    
    	cout << "The string form:" << endl;
    	for (auto i : str)			//	A range for on the variable str. i is type char.
    		print(cout, i) << endl;	//	Calls the print function passing the argument cout and the current character in str.
    
    	cout << "The vector form:" << endl;
    	for (auto i : cvec)		//	A range for on the variable cvec, which is a vector of characters. i is type char.
    		print(cout, i) << endl;		//	Calls the print functing passing the argument cout and the current character in the cvec
    
    	cout << "The array form:" << endl;
    	for (auto i : carr)		//	A range for on the variable, which is an array. Again, i is type char.
    		print(cout, i) << endl;		//	Calls the print function passing the argument cout and the current character in carr.
    }
    Last edited by InunoTaishou; 11-06-2014 at 06:06 PM.

  3. #3
    InunoTaishou's Avatar
    Join Date
    Jan 2009
    Gender
    male
    Location
    The Internet
    Posts
    446
    Reputation
    20
    Thanks
    950
    My Mood
    Relaxed
    Alright I gave you the solution on how to do it but I figured I should break down your code and help you understand why yours does what it does.
    Cleaned it up a little bit but left it, mostly, the same.
    Code:
    #include <iostream>
    
    using namespace std;
    
    char a('A'), b('B'), c('C'), d('D'), generator;	//	Initialize these outside of any scope so they're able to be used in every function.
    int count2(0);		//	Initialize outside of any scope so they're able to be used in every function.
    
    int incre(int a, int b, int c, int d, int count2 = 0)
    {
    
    	switch (count2)
    	{
    	case 1:
    		a = a;
    		break;
    	case 2:
    		a = b;
    		break;
    	case 3:
    		b = c;
    		break;
    	default:
    		c = d;
    
    	}
    	return count2++;
    }
    
    int main()
    {
    
    	generator = a;
    	for (int i = 0, count2 = 0; i < 4; i++)
    	{
    		incre(a, b, c, d, count2);
    		count2++;
    
    		cout << generator;
    	}
    	return 0;
    }
    Let's start from the top. You initially initialized each variable in the main scope, calling upon them and changing them in incre doesn't actually work, all you do when you call incre(a, b, c, d, count2); is pass the values of each of these variables, you aren't passing the variables themselves. Also, you make a second set of each of these variables in incre with the same names, it's not invalid but it's confusing. and
    Code:
     int count2 = 0
    I don't even.... You're passing count2 in the for loop which doesn't even do anything (You don't define parameters, you just declare them). And you don't need to do count++ inside the for loop, you can do it right next to i++ the same way you initialized i and count2.

    Now the biggest thing though is your switch. You don't actually modify any of the original variables, at all. It's always going to be A. What you can do, instead of setting a = a, you could return the char variable you passed into the function (which it will convert to char) or you could make the incre function char and return it that way. Also, you have it start at 1, 2, 3, default which means when you pass count2 as 0 it will go right to default and print D. (Remember, everything always starts at 0)

    So, here's the new (simpler) code that will do what you want.
    Code:
    #include <iostream>
    
    using namespace std;
    
    char a('A'), b('B'), c('C'), d('D'), generator = a;	//	Initialize these outside of any scope so they're able to be used in every function.
    //	I removed count2 from here because it's being redefined in the for loop.
    char incre(int a2, int b2, int c2, int d2, int count2)
    {
    	switch (count2)
    	{
    	case (0) :
    		return a2;
    		break;
    	case (1):
    		return b2;
    		break;
    	case (2):
    		return c2;
    		break;
    	case (3):
    		return d2;
    		break;
    	default:
    		cout << "Something broke..." << endl;
    		return 1;
    		break;
    	}
    }
    
    int main()
    {
    	for (int i = 0, count2 = 0; i < 4; i++, count2++)
    	{
    		generator = incre(a, b, c, d, count2);
    
    		cout << generator;
    	}
    	return 0;
    }
    (Also, if that post above using an ostream& function is too complicated, you can essentially just change print(cout, i) to cout << i and it will do the exact same thing.)

  4. #4
    mic44's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    38
    Reputation
    10
    Thanks
    2
    My Mood
    Aggressive
    Omg !!! Thank you very much !! I do not understand the first way but the second make it all clear !

  5. #5
    Mayion's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Location
    Bed
    Posts
    13,504
    Reputation
    4018
    Thanks
    8,372
    My Mood
    Twisted
    Solved, Closed.
    I do not use any type of messenger outside of MPGH.
    Inactive but you can reach me through VM/PM.










     

    Donator - 30 August 2013
    Battlefield Minion - 26 October 2013

    Blackshot Minion - 14 January 2014/16 September 2014
    Minecraft Minion - 7 February 2014/16 September 2014
    WarRock Minion - 23 February 2014
    League of Legends Minion - 21 March 2014

    Minion+ - 15 May 2014
    Other Semi-Popular First Person Shooter Minion - 8 August 2014
    CrossFire Minion - 23 October 2014
    Programming Section Minion - 13 November 2014
    Marketplace Minion - 7 December 2014

    Official Middleman - 7 December 2014 - 27 June 2015
    Moderator - 29 December 2014
    Project Blackout Minion - 10 January 2015
    News Force Interviewer - January 2015
    Steam Games Minion - 21 March 2015
    Dragon Nest Minion - 31 March 2015
    Publicist - April 2015 - 21 September 2015
    Global Moderator - 25 August 2015
    Super User - 13 August 2016



Similar Threads

  1. [Help Request] Hi all, I want tio make a KAVA account, can anyone help me?
    By Daytan70 in forum Alliance of Valiant Arms (AVA) Help
    Replies: 3
    Last Post: 02-20-2013, 12:06 PM
  2. Can anyone help me with making a modded save...(SWF)
    By TTG-React in forum Realm of the Mad God Help & Requests
    Replies: 0
    Last Post: 10-23-2012, 12:40 PM
  3. [Solved] need help in to make hacks work
    By 2lkebir2wy in forum CrossFire Help
    Replies: 6
    Last Post: 08-07-2012, 06:20 PM
  4. [Help Request] can anyone help me make this mod? for cleric
    By ariefariq2 in forum Dragon Nest Help
    Replies: 3
    Last Post: 09-23-2011, 11:07 AM
  5. VB6 HELP HOW TO MAKE IT WORK
    By poon hacker in forum WarRock - International Hacks
    Replies: 3
    Last Post: 10-11-2007, 08:41 PM