LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2023, 09:53 AM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Rep: Reputation: Disabled
how do i declare a whole bunch of integer variables


hi,

i have 14 variables that are integers. i would like to "hard code" them into my c++ code. is there a way to declare them all as integers (as a group) with one command or do i have to declare them each individually as ints in the "usual way"?

please forgive me if i'm not explaining my question very well. i'm very new to coding

thanks!

Last edited by atjurhs; 12-19-2023 at 10:05 AM.
 
Old 12-19-2023, 10:06 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Code:
int var1, var2, var3, var4, ... var1547
But anyway, usually 12 variables should be organized into a struct (or class).
 
Old 12-19-2023, 10:42 AM   #3
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
hi Pan64

yea the way you showed is what i would call "the usual way".

i don't know enough about structures to do it that way, but i do know it's the better way to do it.
 
Old 12-19-2023, 10:49 AM   #4
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,149

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
In c you would declare it like this I think. Then you only have to pass a single pointer to the struct instead of each int separately. I think C++ would be the same. An object isn't hugely different from a struct as I've learned thus far. Initialize the struct in the code with the values and off you go.

Code:
struct int_load {
  int1;
  int2;
  int3;
  // and so forth
};

Last edited by jmgibson1981; 12-19-2023 at 10:52 AM.
 
Old 12-19-2023, 10:58 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Or, if they are the monthly financial data, use an array `int monthly_income[12];
 
Old 12-19-2023, 03:26 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Do you already know about arrays and vectors?
 
Old 12-19-2023, 08:44 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
Well, if you simply have "a bunch of variables and things," a struct is indeed a very convenient way to handle them. For instance, many of my applications will define a static struct common. And I just put "everything that everyone needs to know about and use" in there.

Now, you'll see throughout the code references to things like common.blahblahblah. And this immediately clues the next programmer that blahblahblah is a "common variable," and where to find it. In other words, it's descriptive.

If you go in my code to the place where this struct is defined, you will there find comments. Lots of them. Explaining what each element is and what it's used for. (I'm a very "gabby" coder, and it has served me very well.)

Last edited by sundialsvcs; 12-19-2023 at 08:46 PM.
 
1 members found this post helpful.
Old 12-20-2023, 07:20 AM   #8
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Do you already know about arrays and vectors?
i know a little bit about vectors, but i'm not very good with them, same for structures.

i don't think these are really variables, per se. they have a constant/unchanging value, separated by white space. even though it's not the most eloquent way, i'd like to "hard code" them.

Code:
A  0
B  1
C  2
D  3
etc etc.
 
Old 12-20-2023, 07:50 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
those are constants, and you can check here:
https://www.freecodecamp.org/news/co...const-keyword/
 
Old 12-20-2023, 08:30 AM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
If they are constants, as they certainly appear to be, then they are usually just declared in a mutually-included file. And they traditionally use ALL_UPPERCASE names. Various languages have various accepted ways to deal with "constants."

Last edited by sundialsvcs; 12-20-2023 at 08:32 AM.
 
Old 12-20-2023, 08:39 AM   #11
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
pan64, i think that's exactly what i want to do. now i just have to figure out how to use them in the rest of my code.

from another part of the code, i will get an integer value, and then i need to correlate that integer value with the description string. so if the code gives me a 2 i will correlate it with C.

do i need to write the #define statement as

Quote:
0 A
1 B
2 C
3 D
etc. etc.

Last edited by atjurhs; 12-20-2023 at 08:49 AM.
 
Old 12-20-2023, 08:43 AM   #12
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
If they are constants, as they certainly appear to be, then they are usually just declared in a mutually-included file. And they traditionally use ALL_UPPERCASE names. Various languages have various accepted ways to deal with "constants."
sundialsvcs, yours is probably the preferred way of doing it. with the #define method i know it's just brute force, but i think that will be the easiest way to do it for a beginner like me.
 
Old 12-20-2023, 09:46 AM   #13
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
a friend of mine who knows some c++ says that the compiler won't let me use a number to look up a string even if i do flip them as in post #11.

he said that i should be using a mapping. so now i get to go learn about making a mapping...
 
Old 12-20-2023, 10:01 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
It works, with a little change:
Code:
wrong: 0 A
good:  #define A 0
 
Old 12-20-2023, 10:38 AM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Quote:
#define A 0
Basically with a #define as posted the preprocessor will replace all instances of A with 0 in your source code.

Just a few ideas. Without knowing anything about what you are trying to accomplish mapping in your case can be as simple as:
char string[] = "ABCD...";

So that string[0]=A, string[1]=B and so on. If you have a description string of more then one character you can create an array of strings i.e.

char arr[3][10] = {"string 1", "string 2 ", "string 3"};
so that
arr[0]="string 1"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
have a bunch of letter character else when pressing Alt key + a (bunch) letter BudiKusasi Linux - Software 1 12-07-2021 04:30 AM
A whole bunch of kubuntu related questions, some regarding SATA Zorko Linux - Newbie 3 11-16-2006 01:41 PM
declare and initialize variables in a header file alaios Programming 3 09-04-2005 04:25 AM
Whole bunch of UDP ports open on firewall machine AllenWood Linux - Networking 1 03-07-2001 10:46 AM
Whole bunch of UDP ports open on firewall machine AllenWood Linux - Security 1 03-06-2001 06:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration