Reading data in - getline

these isn't any point in keeping this question around it won't help anyone.

This should do the trick. Note that all sorts of mayhem would happen if you actually tried to deploy this anywhere. This code is very unsafe because it doesn't check for failure to open the file, or for file format, or for sizes of things that are being read, or really .... anything.
#include <stdio.h>
#include <iostream>
struct salesman
{
char firstname[64];
char lastname[64];
char middleinitial[1];
int averagecents;
int totalcents;
};
int main ()
{
const char* inputFilename = "in.txt";
int numberPeople = 0, weeksToHandlePerPerson = 0;
int workweeklength = 5;
int totalcents = 0;
FILE * fileHandle = fopen ( inputFilename, "r" );
fscanf ( fileHandle, "%d", &numberPeople );
fscanf ( fileHandle, "%d", &weeksToHandlePerPerson );
for ( int i = 0; i < numberPeople; ++i )
{
salesman nextsalesman;
fscanf ( fileHandle, "%s", nextsalesman.firstname );
fscanf ( fileHandle, "%s", nextsalesman.middleinitial );
fscanf ( fileHandle, "%s", nextsalesman.lastname );
float t1, t2, t3, t4, t5;
fscanf ( fileHandle, "%f %f %f %f %f", &t1, &t2, &t3, &t4, &t5 );
nextsalesman.totalcents = 100 * ( t1 + t2 + t3 + t4 + t5 );
nextsalesman.averagecents = nextsalesman.totalcents / workweeklength;
totalcents += nextsalesman.totalcents;
std::cout << "salesman " << i << "total: $" << nextsalesman.totalcents / 100 << "." << nextsalesman.totalcents % 100
<< " and average $" << nextsalesman.averagecents / 100 << "." << nextsalesman. averagecents % 100 << std::endl;
}
int averagecents = totalcents / ( numberPeople * weeksToHandlePerPerson );
std::cout << "total for all: " << totalcents / 100 << "." << totalcents % 100 << " and average for all $" <<
averagecents / 100 << "." << averagecents % 100 << std::endl;
return 0;
}

Related

How to determine / set the length of a string from user input (std::cin)

I've try this case but I got stuck in a trouble. I just want to to determine length of a string from user input or from console. But it doesn't work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int N;
string S;
char vocal;
vocal = {'a'||'i'||'u'||'e'||'o'};
cout << "Length of Spaghetti Name: ";
cin >> N;
fflush(stdin);
cout << "Spaghetti Name: ";
cin >> S;
if (S[0] != vocal){
S.erase(0,1);
} else if (S[N] != vocal){
S.erase(N,1);
}
cout << S << endl;
cin.get();
return 0;
}
expected input
9
Carbonara
expected output
arbonara
This program will delete non vocal character in spaghetti names.

If loop not working. When ran it shows duplicate cout.

I am trying to make a random number generator guessing game, which will allow the user to pick a range. After the range is picked the user will try to guess the number until they get it right. I am new to c++ and do not understand why two cout outputs on screen. I am new to c++ and cant figure out why. I am using visual studio 17.
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
int userNum;
char choice;
int numattempts = 0;
int randNumber;
srand(time(0));
cout << "Welcome to the number guessing game\n" << "Please choose what range you want to guess from\n" << " '1' = 0-20\n '2'= 0-50\n '3'= 0-100\n ";
cin >> choice;
if (choice == '1')
{
randNumber = 0 + rand() % 20;
cout << "Enter a number between 0 and 20\n";
cin >> userNum;
cout << randNumber;
do {
if (userNum < randNumber)
{
cout << "Number is Higher\n";
cin >> userNum;
}
else if (userNum > randNumber)
{
cout << "Number is Lower\n";
cin >> userNum;
}
else (userNum == randNumber);
{
cout << "Congrats You got it!\n";
}
} while (userNum != randNumber);
}
you miss another if in the last checking and should not put semicolon there.
try to debug and you'll find that it should be like this
do {
if (userNum < randNumber)
{
cout << "Number is Higher\n";
cin >> userNum;
}
else if (userNum > randNumber)
{
cout << "Number is Lower\n";
cin >> userNum;
}
else if(userNum == randNumber)
{
cout << "Congrats You got it!\n";
}
} while (userNum != randNumber);

squaring an int and showing the last digit - upgrade for a code

Could anyone help me to upgrade my code ?
I bet the function could be done in shorter and more elegant way, could you show me the proper direction or examples how to write that?
Thanks for the replies, here's the code ( written in Codeblocks 16.01):
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int D,a,b,bb,liczba,dlugosc;
int main()
{
cout << "liczba testow: ";
cin >> D;
while(D<11 && D>0){
for (int j=0;j<D;j++){
cin >> a >> b;
if(a<0 | a>1000000000 | b<0 | b>1000000000){
break;
}
liczba =a;
for(int i=1;i<b;i++){
liczba = liczba*a;
}
char aa[10] = {0};
itoa(liczba,aa,10);
string bb = aa;
int dlugosc = bb.length();
cout << bb << endl;
cout << bb[dlugosc-1] << endl;
a = 0;
}
return 0;
}
return 0;
}

Print address of char pointer

int main(){
char a[] = "Ab";
char *ptr = a;
//ptr = a;
cout << &a << a[0] << endl;
cout << ptr << endl;
char c= 'C';
char *pC = &c;
cout << &c << " :: " << pC << endl;
cout << pC << endl;
}
Output :
0x7fffcb399550A
Ab
CAb :: CAb
CAb
Unable to understand last two outputs.
&c and pC are both pointers to a single character.
operator<< requires its operand of type char* to be a pointer to a null-terminated string, that is, to an array of characters that contains a \0 somewhere.
Otherwise the behaviour is undefined.

One error till it works...how to fix?

#include <iostream>
#include <iomanip>
using namespace std;
int main () // print to console: 3.0*5.0=15.00
{
double a;
double b;
a =(3.0);
b =(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "------" << endl;
cout << fixed << setprecision (2) << a*b << "\n" << endl;
return 0;
}
int calculate () // print to console: (7.1*8.3)-2.2=56.73
{
double a;
double b;
double c;
a = (7.1);
b = (8.3);
c = (2.2);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "- " << c << "\n" << endl;
cout << "------" << endl;
cout << setprecision(2) << (a*b)-c << "\n" << endl;
}
int calculation () // print to console: 3.2/(6.1*5.0)=0.10
{
double a;
double b;
double c;
a=(3.2);
b=(6.1);
c=(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << b << "*" << c << endl; //how can I use variables instead of using quotes?
cout << "------" << endl;
cout << setprecision(2) << a/(b*c) << "\n" << endl;
system("PAUSE");
return 0;
}
What does this output error mean? How do I fix it? someone please explain this to me. Am I suppose to add: int calculate(int a, int b, int c)?
Output:
(32): error C4716: 'calculate' : must return a value
You've declared your function as one that returns an int value but there's no return statement. Try changing the declaration to void calculate() if you don't need to return a value from it.
The calculate function needs to return a value.
You need to add something like this at the end of calculate:
return (a*b)-c;
It's only because your calculate function is supposed to return an int
and there's no return in your function.
if you don't want to return anything, you can put void calculate() instead

Resources