C++ :: Mapping Strings To Integers - How To Print String Zero

Dec 12, 2013

I'm very very new to maps and am really just trying to hash them out by myself.

If you're mapping strings to integers:

map <string, int> myMap;
myMap[ "zero" ] = 0;
myMap[ "one" ] = 1;

How do I print the string "zero", for instance, from myMap?

cout << myMap.at(0) << endl;

doesn't work. Nor does:

cout << static_cast<string>( myMap.at(0) ) << endl;

I need access to the string using the int and the int using the string. Or just direct access to one or the other. . . It's just confusing that they're technically mapped to one another but I can't really access either of them.

View 4 Replies


ADVERTISEMENT

C++ :: Read Set Of Integers Then Find And Print Sum Of Even And Odd Integers

Apr 21, 2014

I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?

View 2 Replies View Related

C/C++ :: Print More Strings (the Strings May Contain Spaces)?

Feb 12, 2014

I have a problem who must print the sentences who have lenght more than 20 characters. I dont know why, but it prints just the first words. Look what i made.

#include<stdio.h>
#include<conio.h>  
int main()

[Code]....

For instance :

Give the number of sentences : 3

First sentence : I like the website bytes.com
Second sentence : I like more the website bytes.com
Third sentence : bytes.com

After I compile the program it should print the first two sentences.

View 2 Replies View Related

C++ :: Link The Strings To The Integers?

Nov 8, 2014

Code:
#include <iostream>
#include <string>
using namespace std;
int main()

[Code] ......

How do I link the strings to the integers?

View 4 Replies View Related

C++ :: Vector With Strings And Integers

Apr 5, 2013

I have a file where the first column shows letters, second column shows numbers.

How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?

View 13 Replies View Related

C++ :: Adding Strings Of Integers

Jan 29, 2013

I'm trying to write an algorithm for a larger project that will take two strings which are both large integers (only using 10 digit numbers for the sake of this demo) and add them together to produce a final string that accurately represents the sum of the two original strings. I realize there are potentially better ways to have gone about this from the beginning but I am supposed to specifically use strings of large integers as opposed to a long integer.

My thinking was to take the two original strings, reverse them so their ones position, tens position, and so on all line up properly for adding. Then one position at a time, convert the characters from the strings to single integers and add them together and then use that sum as the ones position or otherwise for the final string, which once completed will also be reversed back to the correct order of characters.

Where I'm running into trouble I think is in preparing for the event in which the two integers from the corresponding positions in their strings add to a sum greater than 9, and I would then have carry over some remainder to the next position. For example, if I had 7 and 5 in my ones positions that would add to 12, so I would keep the 2 and add 1 to the tens position once it looped back around for the tens position operation.

I'm not getting results that are in any way accurate and after spending a large amount of time stumbling over myself trying to rectify my algorithm, I am not sure what I need to do to fix this.

#include <iostream>
#include <cstdlib>
#include <string>

[Code]....

View 6 Replies View Related

C :: Read TXT File With Strings And Integers

Nov 15, 2013

While I execute the fileprint function i was able to retrieve the record from the txt file. but at the end of the console im getting randoms number i have tried to understand what causing the problem. I have attached a screenshot....

Code:

void fileprint(){ Code: int c;
struct student{
long id;
char name[20];
int mid1;

[Code] .....

View 7 Replies View Related

C/C++ :: Parsing Integers Values From Strings

Sep 30, 2014

So I have this assignment to read a file in, malloc some arrays, run it through a perceptron and to display the final weights. I have the majority of it already written but this is only my third program in C and I'm more familiar with Java and Python than C.

The problem I'm having is when I read in command line arguments, I can't seem to parse integer values from the strings in argv[i] by using atoi().

I've included the piece of code where I'm trying to 'parse.' I understand atoi convers ascii to integer, but I don't understand if it just gives you the ascii code or the number that it actually represents. I attempt to use atoi on lines 33-35

The input arguments in the command line are:

bob in.csv 100 5 10

int main(int args, char* argv[]){
int ** ra; // array of array of pointers
FILE *ifp; // file pointer
char cc; // char var for reading input from file
int i = 0; // counter

[Code] ....

View 3 Replies View Related

C :: Read Text File With Strings And Integers

Nov 16, 2013

i have prepared a code the read from txt file with values such integers and strings. but the code i have prepared reads only 1 line. how can i make the code to read multiple records from txt file.

input records example in txt file:
9009 James 90

Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int c;
struct student{
}

[code]....

View 4 Replies View Related

C++ :: Getline With Integers / Double / Strings And Delimiters?

Apr 22, 2013

I want to use getline to read stuff from a file that looks like:

Student ID:88152; Grades: 83.67, 92.45, 74.43, 97.95, 76.45; Name:Abercrombie, Neil
Student ID:92485; Grades: 77.65, 92.31, 60.47, 67.12, 99.64; Name:Aderholt, Robert
Student ID:10623; Grades: 37.95, 83.11, 64.46, 74.76, 95.30; Name:Alexander, Rodney
Student ID:76793; Grades: 53.13, 81.02, 83.71, 90.75, 88.92; Name:Baca, Joe

I have to print to the screen the id numbers, the grades and the names

I'm using getline but im having trouble print out the grades

It gets and displays everything except the grades. One of the grades only gets displayed

It should be a while loop, but im just working with the first line for now

ifstream inf;
char fileName[ MAX_STR_LEN ] = "ex.txt";
char testStr[ MAX_STR_LEN ];
int testInt;
double testDouble;

[Code] ....

View 3 Replies View Related

C :: Convert From Strings To Integers And Pass Into Linked List

Feb 11, 2013

I have a problem set where i have to read in numbers from a file as strings, convert from strings to integers, and pass the integers into a linked list, where each integer is a node. This is what I have so far:

Code:
# include <stdio.h>
# include <stdlib.h>
# define MAX_INT_SIZE 10000
typedef struct integer BigInt;
struct integer {

[Code] ......

View 10 Replies View Related

C++ :: Text Based RPG Game - Saving Integers And Strings

Jul 14, 2013

I am working on a text-based RPG game and I want to allow the player to save his progress. So I need to save several integers and a string. And my problem starts here "How can I save integers and load them?". I read the tutorial but I dont understand. I need to write a function to save game?

View 8 Replies View Related

C++ :: Print 10 Integers Per Line?

Oct 28, 2014

The code I have is below. Im able to print them all onto one line but I need to print 10 per line separated by commas and Im not sure how to do that ):

for (int i = 0; i < MAXVALUE; i++){
for (int j = 0; j < counts[i]; j++){
output << i << ",";

View 1 Replies View Related

C :: Reading And Printing In A Specific Format Strings Of Characters And Integers

Feb 23, 2015

I have been skimming and searching but dont know how to word the search just right to find what I need to know. I have written simple stuff with the support of tutorials like weight conversion or loops counting up to a set number. Nothing amazing. The other day I found out how to input and print a string rather than a single text character which i though was bad ass. I even got it to read multiple strings on a single line and found a way to read multiple lines. I can even format it to read both integers and characters on the same line as long as I know the predefined format.

On to my question... How do I read multiple lines with both carecters and integers. for instance:

nissan 1996
toyota 1998
or more comples like
nissan gtr 1996
toyota markii 1998

I want to use
int year;
char make[10]; maybe need to use char make[10][10]; for an array i would guess.
char model[10]; optional for the extra data

but reproduce what i read in a different order. say...
1996 nissan
1998 toyota
vice the original format.

this is what I have tried.

Code: scanf("%s %s", &make,&year);

//The way I seen to read multiple lines was on here

scanf("%[^/t]", %make);

But this wont let me separate the two into two differnet definded data types. Let alone use printf to display them in reverse order.

View 1 Replies View Related

C++ :: Vector Of Integers - How To Print Indicators

Apr 8, 2014

I have a vector of integers, which can take values 0 or 1, say

0 0 0 1 1

I need to print indicators, telling in which position 1-values occur. So

0 0 0 1 0
and
0 0 0 0 1

View 11 Replies View Related

C++ :: Print Out Integers Between 2 And 256 That Are Integer Powers Of 2

Nov 5, 2013

Using a do-while loop, for loop, or while loop. This is what I have so far but i don't know what to do after that.

#include <iostream>
using namespace std;
int main() {
int num;
int min = 2;
int max = 256;
}

Also the results should be printed out with 10 integers per line, right justified in a 5 byte field.

View 1 Replies View Related

C++ :: Reading From Console Multiple Strings Of Unknown Length In Combination With Integers

Oct 10, 2014

I want to read a string of unknown length from stdin. I tried to follow the approach from this link.

[URL]....

My code is like this:

Code:

#include <iostream>
#include <string>
using namespace std;
int n;
cin >> n;
cout << "The value of n is " << n << endl;
string str;
getline(cin, str);
cout << "The entereed string is " << str << endl;

What I have noticed is that if I take integer input from cin (cin >> n in the above code before getline, the control does not stop on getline to take str as input from the console. If I don't do (cin >> n) before getline then the control stops on getline and takes the string as input.

What is the best way to read from console multiple strings of unknown length in combination with the integers?

View 1 Replies View Related

C :: Can't Get Strings To Print When Using For Loop

Mar 6, 2015

I am making a program that takes the user's family names, their ages, and where they live. At the end I will be eventually averaging out their age and also printing the names of anyone who lives in Kansas.

I can't seem to get the Kansas part to work properly though.. When I execute the code, everything else works perfectly, but the Kansas part doesn't even print. Is there something different I need to do when strings are involved rather than integers/floats?

Code:

#include <stdio.h>
int main ()
{
/* variable definition: */

[Code].....

View 6 Replies View Related

C/C++ :: Cannot Get Strings To Print When Using For Loop

Mar 24, 2015

I am making a program that takes the user's family names, their ages, and where they live. At the end I will be eventually averaging out their age and also printing the names of anyone who lives in Kansas.

I can't seem to get the Kansas part to work properly though.. When I execute the code, everything else works perfectly, but the Kansas part doesn't even print. Is there something different I need to do when strings are involved rather than integers/floats?

#include <stdio.h>
int main () {
/* variable definition: */

[Code].....

View 3 Replies View Related

C++ :: Input Three Integers From Keyboard - Print Smallest And Largest Numbers

Nov 18, 2013

I am trying to Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. How do i go about it. What i dont know is how to come up with smallest and largest number .

View 1 Replies View Related

C++ :: Program Read In 15 Integers From User Then Store And Print Out Numbers

Apr 29, 2014

I want to write a program to read in 15 integers from the user, and then store them in a 5x3 array, and print out the numbers as a 3x5 array.

View 7 Replies View Related

C/C++ :: Read Integers Into Array / Sort Numbers And Print Out Occurrence Of Each Number

Apr 6, 2014

My C programming class wants us to write a program to read integers into an array, sort the numbers and print out the occurrence of each number. I've tried everything that I can think of but the core dump is still occurring.

void countValues ( FILE *inf, int list[], int size ); /* Function prototypes. */
void printFrequencies( const int list[], int size );
int main (void) {
int status = EXIT_SUCCESS; /* defaulting status to success. */
FILE *inf = fopen( "numbers.txt", "r" ); /* input data file */

[Code] .....

View 6 Replies View Related

C/C++ :: Program To Read Sequence Of N Integers And Print Number With Maximum Occurrence

Mar 24, 2014

write a program in c to read a sequence of N integers and print the number that appears the maximum number of times in the sequence.

View 1 Replies View Related

C++ :: Print Hexadecimal Strings From Binary

Aug 19, 2013

How to search in C or C++ a hexadecimal patterns within a binary file.

For example:

String1:

Code: 0x44 0x65 0x07

Then, once found, extract those 3 bytes and the next 11 bytes (14 bytes in total).

Finally, from those 14 bytes, print without spaces "bytes 1 to 6", "bytes 6 to 12", and byte "13 to 14".

Then I would like to print last 2 bytes (13 to 14) joined in decimal format.

The output without any conversion would be:

Code:
446507c90688 888000800005 0015
4465072ec918 059173495269 002C
44650700cc01 01811bc90b00 00AB

But the output converting to decimal the last 2 bytes would be:

Code:
446507c90688 888000800005 21
4465072ec918 059173495269 44
44650700cc01 01811bc90b00 171

The sample file is attached, and looks like this:

Code:

06 00 00 80 00 00 00 80 09 3c c9 06 88 88 80 00
80 00 44 65 07 c9 06 88 88 80 00 80 00 05 00 15
37 06 01 00 00 01 00 65 00 00 00 02 00 00 02 00
18 00 00 00 03 00 00 03 00 17 00 00 00 04 00 00

[Code] .....

View 8 Replies View Related

C :: Compare 2 Strings And Print Out 1 If Characters Match And 0 If Not

Oct 29, 2014

This program is supposed to compare 2 strings and print out a 1 if the characters match and a 0 if they dont. It compiles but doesnt give me the correct output.

Code:
#include <stdio.h>
#include <string.h>
void func();
int main () {
func();
return 0;

[Code] ....

View 11 Replies View Related

C/C++ :: Print Array Of Strings Is Printing Null Instead Of Result

Feb 17, 2014

I am trying to print the array of string using this code but the compiler is printing null in the output:

The code is as follows:

char *arr[10];
int i;
for(i=0;i<10;i++)
scanf("%s",*(arr+i));  
for(i=0;i<10;i++)
printf("%s
",*(arr+i));

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved