Here is the code I made, but the test case is not working, it goes wrong when the binary string convert to decimal. please help.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
#include <locale>
using namespace std;
// function for option 1
void decToBin(int number)
{
int array[16];
int i = 0;
for (int counter = 0; counter < 16; counter++)
{
array[counter] = 0;
}
while (number > 0)
{
array[i] = number % 2;
number = number / 2;
i++;
}
for (int counter = 15; counter >= 0; counter--)
{
cout << array[counter];
}
}
// function for option 2
void binToDec(int number)
{
int array[16];
double dec = 0;
int i = 0;
for (int counter = 0; counter < 16; counter++)
{
array[counter] = 0;
}
while (number != 0)
{
array[i] = number % 10;
number = number / 10;
i++;
}
for (int counter = 0; counter < 16; counter++)
{
if (array[counter] != 0)
{
dec += pow(2, counter);
}
}
cout << "The result is " << dec << endl;
}
// negative number
void negat(int number)
{
long array[16];
double dec = 0.0;
int length = 16;
int j = 15;
int a = 0;
//store the number in the array check
for (int counter = 15; counter >= 0; counter--)
{
array[counter] = number % 10;
number = number / 10;
}
// flip the numbers check
for (int counter = 15; counter >= 0; counter--)
{
if (array[counter] == 0)
{
array[counter] = 1;
}
else {
array[counter] = 0;
}
}
// add 1
if (array[length] == 1)
{
while (array[length] == 1)
{
array[length] = 0;
length++;
}
array[length + 1]++;
}
else
{
array[15]++;
}
//reverse array
int temp, i;
for (i = 0; i < 16 / 2; ++i) {
temp = array[16 - i - 1];
array[16 - i - 1] = array[i];
array[i] = temp;
}
/// to dec
for (int counter = 0; counter < 16; counter++)
{
if (array[counter] != 0)
{
dec += pow(2, counter);
}
}
dec = 0 - dec;
cout << "The result is " << dec << endl;
}
void ShowMenu()
{
cout << "Welcome to a 16-bit binary conversion program" << endl;
cout << "Binary conversion program by [Qi Liu]\nPlease choice one out of the three options and input the option number :)" << endl;
cout << "1. Convert a 16 bit unsigned decimal to binary \n2. Convert a string of 16 - bit binary to an unsigned decimal\n3. Exit" << endl;
}
/**********************************************************/
int main()
{
int number;
string ans;
int choice;
bool Quit = false;
char move = 'z';
while (!Quit) {
ShowMenu();
cout << "Enter an option: ";
cin >> move;
switch (move) {
case '1':
int number;
string ans;
cout << "Please enter a number between 0 - 65515. \n";
cin >> number;
decToBin(number);
break;
case '2':
int number;
string ans;
cout << "Please enter signed binary \n";
cin >> number;
if (number / 1000000000000000 == 1) {
negat(number);
}
else {
binToDec(number);
}
break;
case '3': //user wants to quit
Quit = true; //execution continues after the switch
//or do this to end program
//return 0;
break;
default:
cout << "Bad Input, Try again " << std::endl;
break;
}
}
cout << "Thank you for using our program" << endl;
system("pause");
return 0;
}
No need to initialize the char move.
As you told you are facing problem in the logic of binary to decimal conversion, you can use something like this:
That will help.
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num);
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base; num = num / 10 ; base = base * 2; }
Here is the code I made, but the test case is not working, it goes wrong...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
so i have my c++ code and ive been working on this for hours but i cant get it to run im not allowed to use arrays. im not sure how to fix it thank you for the help our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses...
C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...
Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...
I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account: Customer name Customer address City State ZIP code Telephone Account balance Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...