Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. set values to charachter arrays which are entered [modified]

set values to charachter arrays which are entered [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toprogramminguy
    wrote on last edited by
    #1

    hey, is there any way to set values to charachter arrays that you enter for example:

    int main()
    {
    int a1, a2, a3, a4,...........a11, x;

    cin >> a1;............cin >> a11;

    char str[10][40]
    for(x = 0; x < 10; x++){
    gets(str[x]);
    }

    int "str[0]" = a1, "str[1]" = a2,.........................."str[9]" = a11;

    return 0;
    }

    thanks

    modified on Sunday, September 7, 2008 8:06 PM

    V 1 Reply Last reply
    0
    • T toprogramminguy

      hey, is there any way to set values to charachter arrays that you enter for example:

      int main()
      {
      int a1, a2, a3, a4,...........a11, x;

      cin >> a1;............cin >> a11;

      char str[10][40]
      for(x = 0; x < 10; x++){
      gets(str[x]);
      }

      int "str[0]" = a1, "str[1]" = a2,.........................."str[9]" = a11;

      return 0;
      }

      thanks

      modified on Sunday, September 7, 2008 8:06 PM

      V Offline
      V Offline
      Vincen Wang
      wrote on last edited by
      #2

      use the pointer:

      int a[10];
      for(x=0;x<10;x++)
      {

      = a[x];

      }

      You know some birds are not meant to be caged, their feathers are just too bright.

      T 1 Reply Last reply
      0
      • V Vincen Wang

        use the pointer:

        int a[10];
        for(x=0;x<10;x++)
        {

        = a[x];

        }

        You know some birds are not meant to be caged, their feathers are just too bright.

        T Offline
        T Offline
        toprogramminguy
        wrote on last edited by
        #3

        #include "stdafx.h"
        #include "iostream"
        #include "cstring"
        using namespace std;

        int main()
        {
        int x, y;
        char ppl[5][80];

        for(y = 0; y < 5; y++){
            gets\_s(ppl\[y\]);
        }
        
        for(x = 0; x < y; x++){
        		cout << ppl\[x\] << "\\n";
        }
        
        int a\[5\];
        for(x = 0;x < y; x++){  
        	\*ppl\[x\] = a\[x\];
        }
        
        for(x = 0;x < y; x++){
        		cout << a\[x\] << "\\n";
        }
        
        
        
        
        
        	    
        
        
        	return 0;
        

        }

        compile this and see what the values for each of the strings are, it doesn't work. and how do you output the values when the variable name is the same as the name for the string, for example if you want to display the strings showing what their values are.

        V 1 Reply Last reply
        0
        • T toprogramminguy

          #include "stdafx.h"
          #include "iostream"
          #include "cstring"
          using namespace std;

          int main()
          {
          int x, y;
          char ppl[5][80];

          for(y = 0; y < 5; y++){
              gets\_s(ppl\[y\]);
          }
          
          for(x = 0; x < y; x++){
          		cout << ppl\[x\] << "\\n";
          }
          
          int a\[5\];
          for(x = 0;x < y; x++){  
          	\*ppl\[x\] = a\[x\];
          }
          
          for(x = 0;x < y; x++){
          		cout << a\[x\] << "\\n";
          }
          
          
          
          
          
          	    
          
          
          	return 0;
          

          }

          compile this and see what the values for each of the strings are, it doesn't work. and how do you output the values when the variable name is the same as the name for the string, for example if you want to display the strings showing what their values are.

          V Offline
          V Offline
          Vincen Wang
          wrote on last edited by
          #4

          toprogramminguy wrote:

          for example if you want to display the strings showing what their values are.

          toprogramminguy wrote:

          << "\n";

          You have made it here.

          toprogramminguy wrote:

          int a[5];

          initialize it just like cin>>a[i]..... Well,if you do really want to convert from "int (like a[x])" to "char [](like ppl[x])",what you have related before.(like "str[0]" = a1,"str[1] = a2............").(The compiler does not allow you to do like this ,but you do.) You can try this:

          sprintf(str[i],"%d",a[i]);//#include "stdio.h"
          //sprintf(str[i],"%c",a[i]);

          You know some birds are not meant to be caged, their feathers are just too bright.

          modified on Monday, September 8, 2008 10:25 PM

          T 1 Reply Last reply
          0
          • V Vincen Wang

            toprogramminguy wrote:

            for example if you want to display the strings showing what their values are.

            toprogramminguy wrote:

            << "\n";

            You have made it here.

            toprogramminguy wrote:

            int a[5];

            initialize it just like cin>>a[i]..... Well,if you do really want to convert from "int (like a[x])" to "char [](like ppl[x])",what you have related before.(like "str[0]" = a1,"str[1] = a2............").(The compiler does not allow you to do like this ,but you do.) You can try this:

            sprintf(str[i],"%d",a[i]);//#include "stdio.h"
            //sprintf(str[i],"%c",a[i]);

            You know some birds are not meant to be caged, their feathers are just too bright.

            modified on Monday, September 8, 2008 10:25 PM

            T Offline
            T Offline
            toprogramminguy
            wrote on last edited by
            #5

            Thanks ill try that but can you set each of the strings be a variable name and then set a value, as opposed to setting a value to a variable name then setting the string equal to that variable name, if you can understand me and my terrible way of explaining things. I'm only learning c++ by myself so i haven't got a clue really thanks again

            V 1 Reply Last reply
            0
            • T toprogramminguy

              Thanks ill try that but can you set each of the strings be a variable name and then set a value, as opposed to setting a value to a variable name then setting the string equal to that variable name, if you can understand me and my terrible way of explaining things. I'm only learning c++ by myself so i haven't got a clue really thanks again

              V Offline
              V Offline
              Vincen Wang
              wrote on last edited by
              #6

              #include <string>//not <string.h>
              using namespace std;
              string str1,str2;
              str1 = "this is my first string.";
              str2 = "this is my second string."
              cout<<str1<<endl;
              cout<<str2<<endl;

              String is a class in boost c++ library,and it has many member functions to deal with string,you can do a search about it,or find a book contains chapter string.

              You know some birds are not meant to be caged, their feathers are just too bright.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups